WPF用户控件 - 一个用户控件的DataGrid的SelectedItem绑定到的ItemSource到DataGrid中的用户控件外 [英] WPF UserControl - SelectedItem of a Usercontrol DataGrid to bind to a ItemSource to DataGrid outside the UserControl

查看:122
本文介绍了WPF用户控件 - 一个用户控件的DataGrid的SelectedItem绑定到的ItemSource到DataGrid中的用户控件外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF用户控件知识就像一个小时了。所以,请原谅我,如果有大量的教程或/和答案对SO有关这个问题(说实话,我不认为这是可以做到,需要重新做代码...因此为什么我想我问)

所以创建用户控件之前,我有这样的fliters基于什么文本,用户在文本框中键入客户数据网格。一旦发现,该过滤器DataGrid的的SelectedItem然后用来绑定到包含一个新的集合新的DataGrid。

So before creating a UserControl, I had a datagrid that fliters customers based on what text the user typed in a Textbox. Once found, the SelectedItem of that filter DataGrid is then used to bind to a new DataGrid containing a new collection.

于是....

过滤DataGrid的XAML

Filter DataGrid XAML

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

一旦用户选择在网格中的客户,

Once the User selects a Customer in that grid,

新的DataGrid将包含基于对SelectedCustomer属性行

a new DataGrid would contain rows of properties based on the SelectedCustomer

ItemsSource="{Binding SelectedCustomer.CustomerOrders}"

都很好,它的工作原理。

All well and good and it works.

不过,我将使用这个筛选客户结果的功能很多在我的项目,所以我创建了一个用户控件中过滤DataGrid的工作。

However, I am going to use this Filter Customer results functionality a lot in my project, so I have created a UserControl in which the filter DataGrid is working.

我已经把这个用户控件在视图中,因此问题是我需要什么则selectedItem是在该用户有界到视图一个DataGrid。 (如上面)

I have put this UserControl in a view, so the problem is I need whatever the selectedItem is in the Usercontrol to be bounded to a DataGrid in the view. (Like above)

所以我需要在视图像这样在DataGrid中。

So I need something like this in the DataGrid in the View.

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

好了一点长篇大论,但我希望你明白这个问题,我已经给了手头的主题足够的知识。如果我做错了什么,请告诉我,只是投下来的问题。

Ok a bit long winded but I hope you understand the problem, and I have given enough knowledge on the subject at hand. If I done something wrong, please tell me and just down vote the question.

干杯

推荐答案

您可以在一个新的依赖属性添加到您的自定义的用户控件并绑定您的数据网格的项目源的属性。请确保您的用户控件的数据表格处理选择更改事件,并设置依赖属性来选择的项目。

You can add a new dependency property to your custom usercontrol and bind your datagrid items source to that property. Make sure to handle selection changed event on your user control's data grid and set the dependency property to the selected item.

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



处理选择更改事件

handle the selection changed event

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

        }

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



,然后绑定到

and then bind to

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

这篇关于WPF用户控件 - 一个用户控件的DataGrid的SelectedItem绑定到的ItemSource到DataGrid中的用户控件外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆