如何在每个窗口中找到第一个元素(网格元素)? 我需要得到窗口孩子。但我不想使用“VisualStateHelper”。 谢谢你的帮助。
答案 0 :(得分:1)
this.Content应该做的伎俩
public MainWindow()
{
InitializeComponent();
Grid grid = this.Content as Grid; //grid will be null if the first child is not a Grid
if(grid != null)
{
//do the work you want to do
}
}
答案 1 :(得分:1)
这是一个静态完成所有操作的解决方案。你可以从任何地方打电话。
using System.Windows;
...
/// <summary>
/// Gets the child <see cref="System.Windows.Grid"/> instances of all windows that have them as the first child.
/// </summary>
/// <returns>An enumerable of tuples pairing the window instance with the child grid.</returns>
public static IEnumerable<Tuple<Window, Grid>> GetAllGrids()
{
foreach (var window in Application.Current.Windows)
{
Grid grid = this.Content as Grid; //grid will be null if the first child is not a Grid
if (grid != null)
{
yield return Tuple.Create(window, grid);
}
}
}