C# 在WPF中的客户端应用程序中设置用户控件的datacontext

C# 在WPF中的客户端应用程序中设置用户控件的datacontext,c#,wpf,user-controls,C#,Wpf,User Controls,我已经创建了一个第三方用户控件,现在想在客户端应用程序中使用它。我在将DataContext设置为控件时遇到问题 用户控制:- <Grid> <DataGrid x:Name="dataGrid" Width="400" Height="400" ItemsSource="{Binding DataTableSource}"/> </Grid> 如何在客户端应用程序中设置DataTableSource <Grid> <co

我已经创建了一个第三方用户控件,现在想在客户端应用程序中使用它。我在将
DataContext
设置为控件时遇到问题

用户控制:-

<Grid>
    <DataGrid x:Name="dataGrid" Width="400" Height="400"  ItemsSource="{Binding DataTableSource}"/>
</Grid>
如何在客户端应用程序中设置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. 

}

公共主窗口()
{
初始化组件();
ds=provider.GetDataSet();
table=ds.Tables[0];
//我必须将表设置为DataTableSource。在这里我无法访问DataTableSource。
}

我无法访问myCustGrid.DataTableSource。它说CustomGridControl不包含
DataTableSource
的定义。为什么?

我已尝试将您的自定义作为从网格继承:

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));
}
这是xaml:

<local:CustomGridControl x:Name="testCustomGrid" />
我也能够从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));
}
//
///CustomGridControl.xaml的交互逻辑
/// 
公共部分类CustomGridControl:UserControl,INotifyPropertyChanged
{
公共CustomGridControl()
{
初始化组件();
}
私有数据表DataTableSource;
公共数据表数据源
{
获取{return\u DataTableSource;}
设置
{
_DataTableSource=值;
PropertyChanged(这是新的PropertyChangedEventArgs(“DataTableSource”);
}
}
公共事件PropertyChangedEventHandler PropertyChanged=委托{};
公共数据表DataTableSourceVersion2
{
get{return(DataTable)GetValue(DataTableSourceVersion2Property);}
set{SetValue(DataTableSourceVersion2Property,value);}
}
//使用DependencyProperty作为DataTableSourceVersion2的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性DataTableSourceVersion2Property=
Register(“DataTableSourceVersion2”、typeof(DataTable)、typeof(CustomGridControl)、new PropertyMetadata(null));
}
这是XAML:

  <local:CustomGridControl DataTableSource="" DataTableSourceVersion2=""/>


因此,两个版本的DataSourceTable都可用

我把它改成了网格。仍然无法访问它:/我的用户控件中除了
DataGrid
之外还有其他控件。好的,让我试试。好的,我一定是做错了什么。我仍然无法访问它。谢谢你的帮助。你在客户端应用程序中使用了用户控件,对吗?@nan我正在主窗口附近创建新的WPF用户控件。在同一名称空间中。答案中的部分类就是确切的代码隐藏。
 /// <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));
}
  <local:CustomGridControl DataTableSource="" DataTableSourceVersion2=""/>