2

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?

1 Answer 1

1

I've tried your custom as inheriting from 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));
}

This the xaml:

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

And i am able to use from codebehing like this:

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

I've been able to inherit from a UserControl too:

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

And this is the XAML:

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

So, both version of the DataSourceTable are available.

Sign up to request clarification or add additional context in comments.

6 Comments

I changed it to Grid. Still not able to access it :/ And I have other controls apart from DataGrid in my User Control.
Okay Let me try this out .
Ok I must be doing something wrong. I am still unable to access it. Thanks for your help anyway.
You used the user control in a client application right?
@nan i am creating the new WPF UserControl near the MainWindow. In the same namespace. The partial class from the answer is the exact codebehind.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.