WPF UserControl - SelectedItem Usercontrol DataGrid for binding to ItemSource for DataGrid outside UserControl

Hi, My knowledge of WPF UserControl is like an hour. So please forgive me if there are many lessons or answers to SO on this question (Honestly, I don’t think it can be done, and you will need to redo the code ... hence why I thought I was asking)

So, before creating UserControl, I had a datagrid that wraps clients based on the text entered by the user in the text box. Once discovered, the SelectedItem of this DataGrid filter is then used to bind to a new DataGrid containing the new collection.

So....

DataGrid XAML Filter

SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource cvsCustomers}}"

As soon as the User selects the Customer in this grid,

new DataGrid will contain property rows based on SelectedCustomer

ItemsSource="{Binding SelectedCustomer.CustomerOrders}"

, .

, " " , UserControl, DataGrid.

UserControl , , , selectedItem Usercontrol, DataGrid . ( )

- DataGrid .

ItemsSource="{Binding ElementName=myUserControl, Path=SelectedCustomer.CustomerOrders}"

, , , . - , .

+3
1

datagrid . .

   public object MySelectedItem
        {
            get { return (object)GetValue(MySelectedItemProperty); }
            set { SetValue(MySelectedItemProperty, value); }
        }

    // Using a DependencyProperty as the backing store for MySelectedItem.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MySelectedItemProperty =
        DependencyProperty.Register("MySelectedItem", typeof(object), typeof(YOURUSERCONTROLTYPE), new UIPropertyMetadata(null));

   public YourUserControl()
        {
            InitializeComponent();
            dgv.SelectionChanged += dgv_SelectionChanged; 

        }

    void dgv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MySelectedItem = dgv.SelectedItem;
        }

ItemsSource="{Binding ElementName=myUserControl, Path=MySelectedItem.CustomerOrders}"
+3

Source: https://habr.com/ru/post/1546780/


All Articles