[英]Setting up datacontext of the user control in client application in WPF
I have created a third party user control and now want to use it in a client application.我创建了一个第三方用户控件,现在想在客户端应用程序中使用它。 I am having a problem with setting the
DataContext
to the control.我在将
DataContext
设置为控件时遇到问题。
UserControl :-用户控制:-
<Grid>
<DataGrid x:Name="dataGrid" Width="400" Height="400" ItemsSource="{Binding DataTableSource}"/>
</Grid>
Code behind:-背后的代码:-
public partial class CustomGridControl : UserControl
{
public CustomGridControl()
{
InitializeComponent();
this.DataContext = this;
}
public DataTable DataTableSource
{
get
{
return (DataTable)GetValue(GridSource);
}
set
{
SetValue(GridSource, value);
}
}
public static readonly DependencyProperty GridSource = DependencyProperty.Register("DataTableSource", typeof(DataTable), typeof(CustomGridControl), new PropertyMetadata(null));
}
How do I set DataTableSource in the client application?如何在客户端应用程序中设置 DataTableSource?
<Grid>
<controls:CustomGridControl Name="myCustGrid" />
</Grid>
public MainWindow()
{
InitializeComponent();
ds = provider.GetDataSet();
table = ds.Tables[0];
//I have to set the table as DataTableSource. Here I am unable to access DataTableSource.
}
I am unable to access myCustGrid.DataTableSource.我无法访问 myCustGrid.DataTableSource。 It says CustomGridControl does not contain a definition for
DataTableSource
.它说 CustomGridControl 不包含
DataTableSource
的定义。 Why?为什么?
I've tried your custom as inheriting from Grid:我试过你的自定义继承自 Grid:
public partial class CustomGridControl : Grid
{
public DataTable DataTableSource
{
get
{
return (DataTable)GetValue(GridSource);
}
set
{
SetValue(GridSource, value);
}
}
public static readonly DependencyProperty GridSource = DependencyProperty.Register("DataTableSource", typeof(DataTable), typeof(CustomGridControl), new PropertyMetadata(null));
}
This the xaml:这是xaml:
<local:CustomGridControl x:Name="testCustomGrid" />
And i am able to use from codebehing like this:我可以像这样从代码行为中使用:
testCustomGrid.DataTableSource = new DataTable("dtName");
I've been able to inherit from a UserControl too:我也能够从 UserControl 继承:
/// <summary>
/// Interaction logic for CustomGridControl.xaml
/// </summary>
public partial class CustomGridControl : UserControl, INotifyPropertyChanged
{
public CustomGridControl()
{
InitializeComponent();
}
private DataTable _DataTableSource;
public DataTable DataTableSource
{
get { return _DataTableSource; }
set
{
_DataTableSource = value;
PropertyChanged(this, new PropertyChangedEventArgs("DataTableSource"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public DataTable DataTableSourceVersion2
{
get { return (DataTable)GetValue(DataTableSourceVersion2Property); }
set { SetValue(DataTableSourceVersion2Property, value); }
}
// Using a DependencyProperty as the backing store for DataTableSourceVersion2. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataTableSourceVersion2Property =
DependencyProperty.Register("DataTableSourceVersion2", typeof(DataTable), typeof(CustomGridControl), new PropertyMetadata(null));
}
And this is the XAML:这是 XAML:
<local:CustomGridControl DataTableSource="" DataTableSourceVersion2=""/>
So, both version of the DataSourceTable are available.因此,两个版本的 DataSourceTable 都可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.