2015-06-12 3 views
2

Я создал сторонний пользовательский элемент управления и теперь хочу использовать его в клиентском приложении. У меня возникла проблема с установкой DataContext на управление.Настройка datacontext пользовательского элемента управления в клиентском приложении в WPF

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

Я попробовал ваш обычай, как наследование из сетки:

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" /> 

И я могу использовать из codebehing так:

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 доступны.

+0

Я изменил его на сетку. Все еще не в состоянии получить к нему доступ:/И у меня есть другие элементы управления, кроме «DataGrid» в моем User Control. – nan

+0

Хорошо Позвольте мне попробовать это. – nan

+0

Хорошо, я должен делать что-то неправильно. Я все еще не могу получить к нему доступ. Спасибо за вашу помощь. – nan

Смежные вопросы