我有主要的 DataGrid
用于显示文档列表。
此 MainGrid
具有在 XAML
中指定的 DataGrid.RowDetailsTemplate
。
此 DataGrid.RowDetailsTemplate
包含一个(内部或嵌套的)DataGrid
。
所以 MainGrid
的每一行都包含 DataGrid.RowDetailsTemplate
和内部 DataGrid
。
我需要获取仅具有 MainGrid
引用的所有内部(嵌套)DataGrid
的列表。
我试过 Visual/Logil Tree 助手,但两者都没有为 GetChildren 调用返回任何内容...
从DataGrid.RowDetails
获取嵌套DataGrid
的方法是什么?
复现步骤: 1) 在 Visual Studio 2013 或更高版本中创建 Windows 桌面 -> WPF 应用程序(空) 2) 使用下面的代码示例:
主窗口.xaml
<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>
主窗口.xaml.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
}
}
刷新命令.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace ExampleNestedGrid
{
public class RefreshCommand : ICommand
{
private ObservableCollection<Document> Documents;
private System.Windows.Controls.DataGrid MainGrid;
#region public methods
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
#endregion
#region public methods
public RefreshCommand(ObservableCollection<Document> Documents, System.Windows.Controls.DataGrid MainGrid)
{
// TODO: Complete member initialization
this.Documents = Documents;
this.MainGrid = MainGrid;
}
public void Execute(object parameter)
{
Documents.First().LinkedEmployees.First().Status = !Documents.First().LinkedEmployees.First().Status;
ICollectionView view = CollectionViewSource.GetDefaultView(Documents);
view.Filter = (item) => item != null;
MainGrid.ItemsSource = view;
var childGrids = FindVisualChildren<DataGrid>(MainGrid);
foreach (DataGrid childGrid in childGrids)
{
MessageBox.Show(childGrid.Name);
}
}
public bool CanExecute(object parameter)
{
return Documents != null && MainGrid != null;
}
#endregion
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;
}
}
}
}
}
}
3) 开始申请。单击第一行。行详细信息应展开。
4) 按第一列对主行进行排序
5) 按第二列对行详细信息进行排序
6) 点击刷新按钮
7) 检查排序指示器是否消失。
最佳答案
您只能获取对 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;
}
}
}
}
编辑:如果您重置其ItemsSource
,则调用DataGrid
的UpdateLayout()
方法。如果我在父 DataGrid
中选择一行并单击“刷新”按钮,则该命令的 Execute
方法的实现对我有用:
public void Execute(object parameter)
{
Documents.First().LinkedEmployees.First().Status = !Documents.First().LinkedEmployees.First().Status;
ICollectionView view = CollectionViewSource.GetDefaultView(Documents);
view.Filter = (item) => item != null;
MainGrid.ItemsSource = view;
MainGrid.UpdateLayout();
List<DataGrid> childGrids = new List<DataGrid>();
foreach (var item in MainGrid.Items)
{
var container = MainGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (container != null)
{
childGrids.AddRange(FindVisualChildren<DataGrid>(container));
}
}
}
关于c# - WPF:从 DataGrid.RowDetailsTemplate 获取指定控件的所有列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44762638/