0
我想知道在x:名稱網格的孩子在這種情況下:如何獲得網格的x:名稱?
<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」,而不是我想知道在這種情況下,「容器1」的x:名稱,那麼我請問,如果你有任何建議,我可以接收網格的x:名稱。
預先感謝您。
真誠
你嘗試'ttt.Name'? – dkozl
@dkozl是的,你是對的!!!非常感謝你......如果你回覆我的答案,我會標記它! – JayJay