WPF DataGrid绑定到XAML中的DataTable [英] WPF DataGrid binding to DataTable in XAML
问题描述
我对WPF / XAML完全陌生。我正在尝试编制XAML代码以将DataTable绑定到DataGrid。我所拥有的是实现INotifyPropertyChanged的自定义DataContainer类的实例。此类具有一个属性:
I'm completely new to WPF/XAML. I'm trying to work out XAML code to bind a DataTable to DataGrid. What I have is an instance of custom DataContainer class which implements INotifyPropertyChanged. This class has a property:
private DataTable totalsStatus = new DataTable();
public DataTable TotalsStatus
{
get { return totalsStatus; }
set
{
totalsStatus = value;
NotifyPropertyChanged("TotalsStatus");
}
}
现在,在我MainWindow的C'tor中
now, in the C'tor of my MainWindow I have this, which works like a charm:
Binding b = new Binding();
b.Source = DataContainer;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b.Path = new PropertyPath("TotalsStatus");
DataGridMain.SetBinding(DataGrid.ItemsSourceProperty, b);
如何在XAML中进行此绑定?
How do I make this binding in XAML?
推荐答案
您需要使用 objectdataprovider
。
<ObjectDataProvider x:Key="yourdataproviderclass"
ObjectType="{x:Type local:yourdataproviderclass}" />
<ObjectDataProvider x:Key="dtable"
ObjectInstance="{StaticResource yourdataproviderclass}"
MethodName="GetTable"/> <!--here would be the method that returns your datasource-->
然后您可以使用Xp
$ b将其绑定到数据网格$ b
Then you can bind it to your datagrid in XAML with
<DataGrid ItemsSource="{Binding Source={StaticResource dtable}}" ></DataGrid>
虽然在xaml中有不同的绑定方法,所以请多加尝试。
There are different ways to do bindings in xaml though, so play around with it a bit.
这篇关于WPF DataGrid绑定到XAML中的DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!