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.
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?
<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. It says CustomGridControl does not contain a definition for DataTableSource
. Why?
I've tried your custom as inheriting from Grid:
This the xaml:
And i am able to use from codebehing like this:
I've been able to inherit from a UserControl too:
And this is the XAML:
So, both version of the DataSourceTable are available.