我有主DataGrid
用于显示文档列表。
此MainGrid
DataGrid.RowDetailsTemplate
中指定了XAML
。
此DataGrid.RowDetailsTemplate
包含一个(内部或嵌套)DataGrid
s。
因此MainGrid
的每一行都包含内部DataGrid.RowDetailsTemplate
的{{1}}。
我需要获取仅具有DataGrid
引用的所有内部(嵌套)DataGrid
的列表。
我已经尝试过Visual / Logil Tree助手,但两者都没有为GetChildren调用返回任何内容......
从MainGrid
获取嵌套DataGrid
的方式是什么?
Repro步骤: 1)创建Windows桌面 - > Visual Studio 2013或更高版本中的WPF应用程序(空) 2)使用下面的代码示例:
MainWindow.xaml
DataGrid.RowDetails
MainWindow.xaml.cs
<Window x:Class="ExampleNestedGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ToolBar Grid.Row="0" Header="Action:">
<Button x:Name="RefreshBtn" Command="{Binding RefreshCommand}">Refresh</Button>
</ToolBar>
<DataGrid x:Name="MainGrid" ItemsSource="{Binding Documents}" AutoGenerateColumns="False" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name, Mode=OneWay}" Width="*"/>
<DataGridTextColumn Binding="{Binding Number, Mode=OneWay}" Width="*"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid>
<DataGrid x:Name="NestedGrid" ItemsSource="{Binding LinkedEmployees}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id, Mode=OneWay}" Width="150"/>
<DataGridTextColumn Binding="{Binding Name, Mode=OneWay}" Width="150"/>
<DataGridTextColumn Binding="{Binding Status, Mode=OneWay}" Width="150"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
RefreshCommand.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ExampleNestedGrid
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new DisplayViewModel(MainGrid);
}
}
public class DisplayViewModel : PropertyBase
{
private DataGrid MainGrid;
public DisplayViewModel(DataGrid MainGrid)
{
this.MainGrid = MainGrid;
Documents = new ObservableCollection<Document>();
LinkedEmployee empl1 = new LinkedEmployee("1", "Ben");
LinkedEmployee empl2 = new LinkedEmployee("2", "John");
Document doc = new Document("first", "111");
doc.LinkedEmployees.Add(empl1);
doc.LinkedEmployees.Add(empl2);
Documents.Add(doc);
RefreshCommand = new RefreshCommand(Documents, MainGrid);
}
public ObservableCollection<Document> Documents { get; set; }
public ICommand RefreshCommand { get; set; }
}
public sealed class LinkedEmployee : PropertyBase
{
public LinkedEmployee(string id, string name)
{
_id = id;
_name = name;
}
public void Update(string name)
{
Name = name;
}
private string _id;
private string _name;
private bool _status;
public bool Status
{
get { return _status; }
set
{
_status = value;
OnPropertyChanged();
}
}
public string Id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged();
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
}
public class Document : PropertyBase
{
public Document(string name, string number)
{
_name = name;
_number = number;
LinkedEmployees = new ObservableCollection<LinkedEmployee>();
}
public void Update(string number)
{
Number = number;
}
private string _name;
private string _number;
public virtual string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
public virtual string Number
{
get { return _number; }
set
{
_number = value;
OnPropertyChanged();
}
}
public ObservableCollection<LinkedEmployee> LinkedEmployees { get; set; }
}
public abstract class PropertyBase : INotifyPropertyChanged
{
#region public properties
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region protected methods
protected void OnPropertyChanged([CallerMemberName]string caller = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(caller));
}
#endregion
}
}
3)开始申请。单击第一行。行详细信息应该扩展。
4)按第一列排序主行
5)按第二列排序行详细信息
6)点击刷新按钮
7)检查分类指示消失。
答案 0 :(得分:1)
您只能获得RowDetailsTemplate
中当前可见元素的引用。试试这个:
private void Button_Click(object sender, RoutedEventArgs e)
{
var childGrids = FindVisualChildren<DataGrid>(MainGrid);
foreach (DataGrid childGrid in childGrids)
{
//...
}
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
修改:如果您重置UpdateLayout()
,请调用DataGrid
的{{1}}方法。如果我在父ItemsSource
中选择一行并单击“刷新”按钮,则此命令的Execute
方法的实现对我有用:
DataGrid