WPF UserControl - UserControl DataGrid 的 SelectedItem 绑定到 UserControl 之外的 DataGrid 的 ItemSource [英] WPF UserControl - SelectedItem of a Usercontrol DataGrid to bind to a ItemSource to DataGrid outside the UserControl

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

问题描述

我的 WPF UserControl 知识已经有一个小时了.所以请原谅我,如果有很多关于这个问题的教程或/和答案(老实说,我认为这不能完成,需要重新编写代码......因此我想我问)

因此,在创建 UserControl 之前,我有一个数据网格,它根据用户在文本框中键入的文本筛选客户.找到后,该过滤器 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.

所以……

过滤数据网格 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.

但是,我将在我的项目中大量使用此过滤客户结果功能,因此我创建了一个 UserControl,过滤器 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.

我已将此 UserControl 放在视图中,所以问题是我需要将 UserControl 中的 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));

处理选择更改事件

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

        }

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

然后绑定到

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

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

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