gpt4 book ai didi

C# wpf,从文本框绑定(bind)到 dataGridColumn

转载 作者:行者123 更新时间:2023-11-30 21:54:39 26 4
gpt4 key购买 nike

我仍然是 wpf 的初学者 - mvvm。我有一个与集合绑定(bind)的数据网格。

我需要用文本框的内容填充第一列。所以每次我添加一个新行时,第一列应该已经有我的文本框的内容。

我该怎么做?

View :

<Grid DataContext="{Binding Source={StaticResource invoice}}">
<StackPanel Orientation="Horizontal">
<TextBox Width="71"
Name="InvoiveNumber"
Text="{Binding ??, Mode=OneWay}">
<!-- My textbox -->
</TextBox>
<DatePicker></DatePicker>
<Label Content="Shop" />
<ComboBox Margin="5"
ItemsSource="{Binding Collection, Source={StaticResource shop}}"
DisplayMemberPath="shop1"
Width="53" />
<Label Content="Supplier" />
<ComboBox Margin="5"
ItemsSource="{Binding Collection, Source={StaticResource supplier}}"
DisplayMemberPath="supplier"
SelectedItem="{Binding Selected, Source={StaticResource supplier}, Mode=TwoWay}"
Width="46" />
</StackPanel>
<DataGrid x:Name="dataGridInvoice"
Margin="5"
Grid.Row="1"
ItemsSource="{Binding Collection}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<!-- My column -->
<DataGridTextColumn x:Name="dataGridTextColumn"
Header="Supplier Invoice Nb"
Binding="{Binding suppInvNumber, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridComboBoxColumn Header="Ref Supplier"
ItemsSource="{Binding Products, Source={StaticResource supplier}, Mode=TwoWay}"
DisplayMemberPath="refsup"
SelectedValueBinding="{Binding refSupp}"
SelectedValuePath="refsup"
Width="*" />
<DataGridTextColumn Header="Quantity"
Binding="{Binding quantity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Prix/MOQ"
Binding="{Binding unitPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Total Price"
Binding="{Binding totalPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
</DataGrid.Columns>
</DataGrid>
<Button x:Name="BtnAdd"
Content="Save"
Command="{Binding SaveCommand}"
Margin="94,0" />
</StackPanel>
</Grid>

View 模型:

public class InvoiceViewModel : ViewModelBase
{
public Context ctx = new Context();

public InvoiceViewModel()
{
Get(false);
}


private ObservableCollection<Invoice> collection;

public ObservableCollection<Invoice> Collection
{
get
{
return collection;
}
set
{
collection = value;
OnPropertyChanged("Collection");
}
}


private Invoice _selected;
public Invoice Selected
{
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("Selected");
}
}

private void Get(bool loadDataFirst)
{
if (loadDataFirst) ctx.Invoices.Load();
Collection = ctx.Invoices.Local;
}


private void Save()
{
ctx.SaveChanges();
}

private void Delete()
{
var id = Selected;
var invoice = (from i in ctx.Invoices
where i.idInvoice == id.idInvoice
select i).SingleOrDefault();

Collection.Remove(invoice);
}

private Invoice _currentItem;
public Invoice CurrentItem
{
get
{
return _currentItem;
}
set
{
_currentItem = value;
OnPropertyChanged("CurrentItem");
}
}


#region "Command"

private ICommand saveCommand;
private ICommand removeCommand;

public ICommand SaveCommand
{
get
{
return saveCommand ?? (saveCommand = new RelayCommand(p => this.Save(), p => this.CanSave()));
}
}


private bool CanSave()
{
return true;
}

public ICommand DeleteCommand
{
get
{
return removeCommand ?? (removeCommand = new RelayCommand(p => this.Delete(), p => this.CanDelete()));
}
}

public bool CanDelete()
{
if (Selected != null)
return true;
else
return false;
}

#endregion

}

最佳答案

首先,感谢您提出如此具有挑战性的问题。由于这个问题,我今天大量使用 DataGrid。

花了一整天的时间,尝试了各种方法完美解决后,问题解决了。

在文本框中输入一些数字,然后单击按钮。该数字将自动出现在任何新行中。仅在文本框中更改此数字不会带来任何更改,因为我没有使用该方法。单击按钮是必须的。我本可以禁用编辑第一列,但保留原样。该解决方案同样适用,无需对 AutoGenerateColumns="True"进行任何更改。

下面的代码可以直接使用:

Window1.xaml

<Window x:Class="WpfDataControls.DataGrid.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="385.714" Width="598.872">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="249*"/>
<RowDefinition Height="79*"/>
</Grid.RowDefinitions>

<TextBox x:Name="tbSupplierInvoiceNumber" HorizontalAlignment="Left" Height="23" Margin="168,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="120"/>
<Button Content="Get invoices" HorizontalAlignment="Left" Margin="306,11,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<DataGrid x:Name="dgrdInvoice" HorizontalAlignment="Left" Margin="10,10,0,0" AutoGenerateColumns="False" CanUserAddRows="True"
VerticalAlignment="Top" Height="229" Width="571"
LoadingRow="dgrdInvoice_LoadingRow">
<DataGrid.Columns>
<!-- My column -->
<DataGridTextColumn x:Name="dataGridTextColumn"
Header="Supplier Invoice Nb"
Binding="{Binding suppInvNumber, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Quantity"
Binding="{Binding quantity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Prix/MOQ"
Binding="{Binding unitPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Total Price"
Binding="{Binding totalPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
</DataGrid.Columns>
</DataGrid>
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Initial suppInvNumber" VerticalAlignment="Top" Width="140"/>
<TextBlock HorizontalAlignment="Left" Margin="111,38,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Change this number and click get invoices to see the change" VerticalAlignment="Top"/>

</Grid>
</Window>

Window1.xaml.cs

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics;

namespace WpfDataControls.DataGrid
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

dgrdInvoice.AddingNewItem += dgrdInvoice_AddingNewItem;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
ViewModel vm = new ViewModel();
dgrdInvoice.ItemsSource = vm.InvoiceCollection;
}

#region Grid events
void dgrdInvoice_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Loaded += dgrdInvoice_Row_Loaded;
}

void dgrdInvoice_Row_Loaded(object sender, RoutedEventArgs e)
{
DataGridRow newRow = ((DataGridRow)sender);

if (newRow.GetIndex() == dgrdInvoice.Items.Count - 1)
{
newRow.Background = new SolidColorBrush(Colors.BlanchedAlmond);
DependencyObject reference = newRow;

while (true)
{
reference = VisualTreeHelper.GetChild(reference, 0);
if (reference is TextBlock)
{
TextBlock f = (TextBlock)reference;
f.Text = Convert.ToInt32(tbSupplierInvoiceNumber.Text).ToString();
break;
}
}
}
}

void dgrdInvoice_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
DataGridRow newRow = (DataGridRow)dgrdInvoice.ItemContainerGenerator.ContainerFromIndex(dgrdInvoice.Items.Count - 1);
DependencyObject reference = newRow;
int invoiceNumber;
while (true)
{
reference = VisualTreeHelper.GetChild(reference, 0);
if (reference is TextBlock)
{
invoiceNumber = Convert.ToInt32(tbSupplierInvoiceNumber.Text);
TextBlock f = (TextBlock)reference;
f.Text = invoiceNumber.ToString();
break;
}
}

e.NewItem = new Invoice() { suppInvNumber = invoiceNumber };
}
#endregion

}

public class ViewModel
{
ObservableCollection<Invoice> _invoiceCollection;
public ObservableCollection<Invoice> InvoiceCollection { get { return _invoiceCollection; } }

public ViewModel()
{
_invoiceCollection = new ObservableCollection<Invoice>();

_invoiceCollection.Add(new Invoice() { suppInvNumber = 1, quantity=120, unitPrice=23, totalPrice=56 });
_invoiceCollection.Add(new Invoice() { suppInvNumber = 3, quantity = 122, unitPrice = 13, totalPrice = 23 });
_invoiceCollection.Add(new Invoice() { suppInvNumber = 4, quantity = 234, unitPrice = 10, totalPrice = 43 });
_invoiceCollection.Add(new Invoice() { suppInvNumber = 6, quantity = 512, unitPrice = 35, totalPrice = 67 });
_invoiceCollection.Add(new Invoice() { suppInvNumber = 7, quantity = 612, unitPrice = 3, totalPrice = 120 });
}
}

public class Invoice
{
public int suppInvNumber { get; set; }
public int quantity { get; set; }
public int unitPrice { get; set; }
public int totalPrice { get; set; }
}
}

关于C# wpf,从文本框绑定(bind)到 dataGridColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32899300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com