如何获取Grid的x:Name?
How to get the x:Name of the Grid?
我想知道 Grid 子项的 x:Name,如本例所示:
<Grid x:Name="one" Grid.Row="0" Margin="49.667,15,15,15">
<Grid x:Name="container1" Background="Red" Margin="10"/>
</Grid>
<Button Content="mov" Foreground="White" x:Name="first" HorizontalAlignment="Left" Margin="8,44.833,0,70.167" Width="29.334" Background="Black" Click="first_Click"/>
这里是我点击时的代码:
private void first_Click(object sender, System.Windows.RoutedEventArgs e)
{
var ttt = FindVisualChild<Grid>(one);
MessageBox.Show(ttt.ToString());
}
private static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
当我点击消息时只显示此内容 "System.Window.Controls.Grid" 而不是我想知道 x:name 在这种情况下 "container1" 然后我请问你是否有任何建议我可以收到网格的x:Name。
提前致谢。
此致
感谢 dkozl 在 OP 的评论中为您提供了答案。我想提供一些额外的信息来补充它。
在 XAML 中向您公开的任何元素都可以在代码隐藏中作为 属性 访问(有一些例外,但在大多数情况下这是正确的)。
<Grid x:Name="one" Grid.Row="0" Margin="49.667,15,15,15">
<Grid x:Name="container1" Background="Red" Margin="10"/>
</Grid>
<Button Content="mov" Foreground="White" x:Name="first" HorizontalAlignment="Left" Margin="8,44.833,0,70.167" Width="29.334" Background="Black" Click="first_Click"/>
如果需要,您可以访问网格属性,例如
private void first_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.one.Background = Brushes.Yellow;
this.one.Margin = new Thickness(0, 5, 10, 25);
}
您也不需要使用可视化树查找,因为您已经为网格提供了名称,前提是代码隐藏与包含这两个网格的视图相关联。
你可以这样做:
private void first_Click(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show(this.container1.Name);
}
我想知道 Grid 子项的 x:Name,如本例所示:
<Grid x:Name="one" Grid.Row="0" Margin="49.667,15,15,15">
<Grid x:Name="container1" Background="Red" Margin="10"/>
</Grid>
<Button Content="mov" Foreground="White" x:Name="first" HorizontalAlignment="Left" Margin="8,44.833,0,70.167" Width="29.334" Background="Black" Click="first_Click"/>
这里是我点击时的代码:
private void first_Click(object sender, System.Windows.RoutedEventArgs e)
{
var ttt = FindVisualChild<Grid>(one);
MessageBox.Show(ttt.ToString());
}
private static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
当我点击消息时只显示此内容 "System.Window.Controls.Grid" 而不是我想知道 x:name 在这种情况下 "container1" 然后我请问你是否有任何建议我可以收到网格的x:Name。
提前致谢。
此致
感谢 dkozl 在 OP 的评论中为您提供了答案。我想提供一些额外的信息来补充它。
在 XAML 中向您公开的任何元素都可以在代码隐藏中作为 属性 访问(有一些例外,但在大多数情况下这是正确的)。
<Grid x:Name="one" Grid.Row="0" Margin="49.667,15,15,15">
<Grid x:Name="container1" Background="Red" Margin="10"/>
</Grid>
<Button Content="mov" Foreground="White" x:Name="first" HorizontalAlignment="Left" Margin="8,44.833,0,70.167" Width="29.334" Background="Black" Click="first_Click"/>
如果需要,您可以访问网格属性,例如
private void first_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.one.Background = Brushes.Yellow;
this.one.Margin = new Thickness(0, 5, 10, 25);
}
您也不需要使用可视化树查找,因为您已经为网格提供了名称,前提是代码隐藏与包含这两个网格的视图相关联。
你可以这样做:
private void first_Click(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show(this.container1.Name);
}