Wpf:如何从嵌套的DataGrid绑定SelectedItem



我不知道如何正确绑定SelectedItem从一个嵌套的DataGrid。在主DataGrid中,我有一个这样的绑定:

SelectedItem="{Binding SelectedElement}"
如果我选择元素DataGrid属性selecteelement从MainVM类设置为所选元素。我在嵌套的DataGrid中有一个类似的绑定:
SelectedItem="{Binding SelectedMyItem}"

但是它根本不起作用-当我选择item在嵌套数据网格中,属性SelectedMyItem为空.

我的问题:
我如何绑定SelectedMyItem属性,以便在DataGrid中选择项目后设置?

我没有从IDE获得任何绑定错误信息。

下面是一个简单的例子来说明我的问题:

类:

using System.Collections.ObjectModel;
namespace NesteGridMVVM
{
public class MyItem
{
public string MyItemName { get; set; }
}

//======================================================================

public class Element
{
private MyItem _selectedItem;
public string ElementName { get; set; }
public ObservableCollection<MyItem> MyItemsList { get; set; } = new ObservableCollection<MyItem>();
//Binded to SelectedItem in nested DataGrid
public MyItem SelectedMyItem
{
get => _selectedItem;
set
{
_selectedItem = value;
//Show, if MyItem was selected - it not work.
System.Diagnostics.Debug.Print($"Selected MyItem: {_selectedItem.MyItemName}");
}
}
}

//======================================================================
public class MainVM
{
private Element _selectedElement;
public ObservableCollection<Element> ElementsList { get; set; } = new ObservableCollection<Element>();
//Binded to SelectedItem in main DataGrid
public Element SelectedElement
{
get => _selectedElement;
set
{
_selectedElement = value;
//Show, if Element was selected - it works OK
System.Diagnostics.Debug.Print($"{_selectedElement.ElementName}");
}
}
//ctor - populate view model
public MainVM()
{
Element elem1 = new Element() { ElementName = "element-01" };
Element elem2 = new Element() { ElementName = "element-02" };
elem1.MyItemsList.Add(new MyItem() { MyItemName = "item-A" });
elem1.MyItemsList.Add(new MyItem() { MyItemName = "item-B" });
elem2.MyItemsList.Add(new MyItem() { MyItemName = "item-C" });
ElementsList.Add(elem1);
ElementsList.Add(elem2);
}
}
}
XAML:

<Window x:Class="NesteGridMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NesteGridMVVM"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="600">
<Window.DataContext>
<local:MainVM /> 
</Window.DataContext>
<DataGrid
x:Name="MainDG"
ItemsSource="{Binding ElementsList}"
AutoGenerateColumns="True"
SelectedItem="{Binding SelectedElement}"
RowDetailsVisibilityMode="Visible">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid
x:Name="NestedDG"
ItemsSource="{Binding MyItemsList}"
AutoGenerateColumns="True"
SelectedItem="{Binding SelectedMyItem}">
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Window>

在我看来,您可以将绑定触发器修改为PropertyChanged:

SelectedItem="{Binding SelectedMyItem, UpdateSourceTrigger=PropertyChanged}"

然而,这只是一个注意事项:

当我在调试器中运行此操作时,嵌套的DataGrid选择在外部DataGrid之前触发。这意味着SelectedMyItemset会在SelectedElement之前触发。当然,这只适用于更改外部DataGrid中的行。

完整.xaml:

<Window x:Class="NesteGridMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NesteGridMVVM"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="600">
<Window.DataContext>
<local:MainVM /> 
</Window.DataContext>
<DataGrid
x:Name="MainDG"
ItemsSource="{Binding ElementsList}"
AutoGenerateColumns="True"
SelectedItem="{Binding SelectedElement}"
RowDetailsVisibilityMode="Visible">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid
x:Name="NestedDG"
ItemsSource="{Binding MyItemsList}"
AutoGenerateColumns="True"
SelectedItem="{Binding SelectedMyItem, UpdateSourceTrigger=PropertyChanged}">
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Window>

最新更新