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

时间:2015-06-12 02:27:26

标签: c# wpf user-controls

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

UserControl: -

<Grid>
    <DataGrid x:Name="dataGrid" Width="400" Height="400"  ItemsSource="{Binding DataTableSource}"/>
</Grid>

代码背后: -

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));
}

如何在客户端应用程序中设置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. 

}

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

1 个答案:

答案 0 :(得分:1)

我已尝试将您的自定义继承自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));
}

这是xaml:

<local:CustomGridControl x:Name="testCustomGrid" />

我可以像这样使用代码发送:

testCustomGrid.DataTableSource = new DataTable("dtName");

我也能从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));
}

这就是XAML:

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

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