locked
WPF binding RRS feed

  • Question

  • Hi Experts,

    I have user control with a data grid. I should be able to call this user control from different views and bind any collection to it and grid should populate data. It works fine when I have only one grid in the view.

    Now i have a situation where in a single WPF view i should have two grids, that means i have to call the user control with data grid twice and different collection should be bound dynamically.

    this is how the data grid looks like within user control's xaml

     <DataGrid ItemsSource="{Binding}" RowHeaderWidth="0" x:Name="CustomDataGrid"></DataGrid>

    Assume my data context class have the following properties

    public class Test
    {
     public List<Staff> Staffs
     {
     get;set;
     }
    
     public List<Department> Departments
     {
     get;set;
     }
    
    }

    So by placing the user control twice in the main window i should be able to bind the List<Staff> to the first grid and List<Department> to the second grid. 

    basically user control with datagrid is a generic control. developer should be able to call from any of their view and bind any collection. 

    Greatly appreciate your help.

    Wednesday, January 30, 2013 6:19 PM

Answers

  • Hi WPF_Boy,

    I think what you need to do is set the different DataContext for different UserControl, and the binding in dataGrid will be same, however, the UserControl's DataContext could not be same.

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by WPF_Boy Friday, February 1, 2013 9:09 AM
    Friday, February 1, 2013 5:58 AM

All replies

  • What you've described is the first step into the world of MVVM.  Your test class is very close to being a viewmodel of CustomData.  Remember though that you must either implement INPC or DPs.  You can have one or more instances of class of which to bind.  They are instaciated either as a static resource in XAML or from the code behind as is needed.

    JP Cowboy Coders Unite!

    Wednesday, January 30, 2013 7:47 PM
  • Hi WPF_Boy,

    I think what you need to do is set the different DataContext for different UserControl, and the binding in dataGrid will be same, however, the UserControl's DataContext could not be same.

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by WPF_Boy Friday, February 1, 2013 9:09 AM
    Friday, February 1, 2013 5:58 AM