0
Label
을 제외한 StackPanel
의 모든 하위 항목을 제거하고 싶지만, 에 가져올 수 없으므로 동적으로 생성 된 Button
을 제거하십시오. 귀하의 예를 들어어떻게 동적으로 생성 된 버튼을 제거 할 수 있습니까?
<StackPanel Name="myStackPanel">
<Label Name="myLabel">Label text</Label>
<TextBlock Name="myTextBlock">TextBlock text</TextBlock>
</StackPanel>
private void button1_Click(object sender, RoutedEventArgs e)
{
Button buttonX= new Button();
buttonX.Name = "ButtonInstall";
buttonX.Content = "Click Me";
buttonX.Width = 150;
buttonX.HorizontalAlignment = HorizontalAlignment.Left;
buttonX.Click += new RoutedEventHandler(buttonX_Click);
myStackPanel.Children.Add(buttonX);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myStackPanel); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(myStackPanel, i);
string controlName = childVisual.GetValue(Control.NameProperty).ToString();
if (childVisual.GetType() != typeof(Label))
{
myStackPanel.Children.Remove((UIElement)childVisual);
}
}
당신이 거기에 추가하면 왜'Children'을 반복하지? (! (stackPanel.Children [i]가 Label 인 경우) for (int i = stackPanel.Children.Count - 1; i> 0; i--) 만 수행하면됩니다. stackPanel.Children.RemoveAt (i); ' – Vlad
우리는 MVVM과 DataTemplates를 가지고 있습니다. – Will
Button2.MouseDown의 trigger에 대해 Visibility.Collapsed를 buttonX로 설정하지 않는 이유는 무엇입니까? –