use*_*422 3 c# wpf xaml user-controls datagrid
嗨,我的 WPF UserControl 知识就像一个小时前一样。所以请原谅我,如果有很多关于这个问题的教程或/和答案(老实说,我不认为这可以完成,需要重新编写代码......因此我想我问)
因此,在创建 UserControl 之前,我有一个数据网格,它根据用户在文本框中键入的文本筛选客户。找到后,该过滤器 DataGrid 的 SelectedItem 将用于绑定到包含新集合的新 DataGrid。
所以....
筛选数据网格 XAML
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource cvsCustomers}}"
Run Code Online (Sandbox Code Playgroud)
一旦用户在该网格中选择了一个客户,
一个新的 DataGrid 将包含基于 SelectedCustomer 的属性行
ItemsSource="{Binding SelectedCustomer.CustomerOrders}"
Run Code Online (Sandbox Code Playgroud)
一切都很好,它的工作原理。
但是,我将在我的项目中大量使用此过滤客户结果功能,因此我创建了一个 UserControl,过滤器 DataGrid 在其中工作。
我已经把这个 UserControl 放在一个视图中,所以问题是我需要将 UserControl 中的 selectedItem 绑定到视图中的 DataGrid。(如上)
所以我在视图中的 DataGrid 中需要这样的东西。
ItemsSource="{Binding ElementName=myUserControl, Path=SelectedCustomer.CustomerOrders}"
Run Code Online (Sandbox Code Playgroud)
好吧,有点啰嗦,但我希望你理解这个问题,并且我已经提供了足够的关于手头主题的知识。如果我做错了什么,请告诉我,然后对这个问题投反对票。
干杯
您可以向自定义用户控件添加新的依赖项属性,并将数据网格项源绑定到该属性。确保处理用户控件数据网格上的选择更改事件并将依赖属性设置为所选项目。
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));
Run Code Online (Sandbox Code Playgroud)
处理选择更改事件
public YourUserControl()
{
InitializeComponent();
dgv.SelectionChanged += dgv_SelectionChanged;
}
void dgv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MySelectedItem = dgv.SelectedItem;
}
Run Code Online (Sandbox Code Playgroud)
然后绑定到
ItemsSource="{Binding ElementName=myUserControl, Path=MySelectedItem.CustomerOrders}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3463 次 |
| 最近记录: |