簡體   English   中英

在 WPF 中的客戶端應用程序中設置用戶控件的數據上下文

[英]Setting up datacontext of the user control in client application in WPF

我創建了一個第三方用戶控件,現在想在客戶端應用程序中使用它。 我在將DataContext設置為控件時遇到問題。

用戶控制:-

<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的定義。 為什么?

我試過你的自定義繼承自 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 都可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM