我在堆叠面板中有两个按钮。最初B1按钮位于顶部,然后是B2。我将从代码中动态更改按钮可见性,这样,当我更改隐藏的B1可见性时,B2将会出现在顶部。如何实现此功能。
<Grid>
<StackPanel >
<Button Content="B1" Height="20" Width="100" Visibility="Visible"/>
<Button Content="B2" Height="20" Width="100" Visibility="Visible"/>
</StackPanel>
</Grid>
答案 0 :(得分:1)
首先删除Statckpanel,然后将其放入网格中即可实现 尝试这样的事情。
<Grid>
<Button Content="B1" Height="20" Width="100" Visibility="Visible" Click="Button_Click" x:Name="B1" />
<Button Content="B2" Height="20" Width="100" Visibility="Visible" x:Name="B2" Click="B2_Click" />
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
B1.Visibility = System.Windows.Visibility.Hidden;
B2.Visibility = System.Windows.Visibility.Visible;
}
private void B2_Click(object sender, RoutedEventArgs e)
{
B2.Visibility = System.Windows.Visibility.Hidden;
B1.Visibility = System.Windows.Visibility.Visible;
}
这应该会给你类似的行为。根据您的使用情况进行更改