我今天早些时候遇到了这个问题,无法找到解决方案.当我在RowDetailstemplate内部的DataGrid中选择一行时,不会设置另一个DataGrid的RowDetailstemplate内的DataGrid的SelectedItem. (很难解释清楚.)
绑定都适用于列表. Mainviewmodel包含一个MyItem对象的ObservableCollection,这是外部DataGrid绑定的内容.
MyItem对象包含一个MyItem2对象的ObservableCollection,它们被正确绑定到内部DataGrid.
MyItem对象还有一个名为SelectedItem2的属性,它应该绑定到内部DataGrid的SelectedItem,但永远不会被设置.
顺便说一句:我正在使用VS2012和.Net 4.5.
例:
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <wpfApplication1:MainWindowviewmodel x:Key="MainVm"/> </Window.Resources> <Grid DataContext="{StaticResource MainVm}"> <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=MyItem1s}"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}"/> </DataGrid.Columns> <DataGrid.RowDetailstemplate> <DataTemplate> <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Item2s}" SelectedItem="{Binding Path=SelectedItem2}"> <DataGrid.Columns> <DataGridTextColumn Header="Item 2 Name" Width="130" Binding="{Binding Path=Name}"/> </DataGrid.Columns> </DataGrid> </DataTemplate> </DataGrid.RowDetailstemplate> </DataGrid> </Grid>
MainWindowviewmodel
public class MainWindowviewmodel : viewmodelBase { public MainWindowviewmodel() { for (int i = 0; i < 10; i++) { var item1 = new MyItem1(); item1.Name = i.ToString(); for (int j = 11; j < 20; j++) item1.Item2s.Add(new MyItem2() { Name = j.ToString() }); MyItem1s.Add(item1); } } private ObservableCollection<MyItem1> _myItem1S = new ObservableCollection<MyItem1>(); public ObservableCollection<MyItem1> MyItem1s { get { return _myItem1S; } set { _myItem1S = value; } } }
MyItems viewmodels
public class MyItem1 : viewmodelBase { private string _name; public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } private ObservableCollection<MyItem2> _item2S = new ObservableCollection<MyItem2>(); public ObservableCollection<MyItem2> Item2s { get { return _item2S; } set { _item2S = value; OnPropertyChanged("Item2s"); } } private MyItem2 _selectedItem2; public MyItem2 SelectedItem2 { get { return _selectedItem2; } set { _selectedItem2 = value; OnPropertyChanged("SelectedItem2"); } } } public class MyItem2 : viewmodelBase { private string _name; public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } }
viewmodelBase
public class viewmodelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string property) { if(PropertyChanged != null) PropertyChanged(this,new PropertyChangedEventArgs(property)); } }
任何帮助,将不胜感激.
谢谢!
解决方法
啊,这很简单,你没有利用你的onpropertychanged
在您的datatemplate上试试这个
<DataTemplate DataType="{x:Type wpfApplication1:MyItem1}"> <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Item2s}" SelectedItem="{Binding Path=SelectedItem2,UpdateSourceTrigger=PropertyChanged}">
……
……
……
你需要updatesourcetrigger,我只是为你的一个绑定设置代码但是你需要在你计划使用propertychanged更新值的地方完成
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。