How control re-position depends on other control visibility in same panel

痞子三分冷 提交于 2020-02-07 07:53:26

问题


I have two buttons inside a stack panel. Initially B1 button is on top, then B2. I will change button visibility dynamically from code so that, when I change B1 visibility hidden, then B2 will come on top. How can I achieve this functionality.

    <Grid>
      <StackPanel >
        <Button Content="B1" Height="20" Width="100" Visibility="Visible"/>
        <Button Content="B2" Height="20" Width="100" Visibility="Visible"/>
     </StackPanel>
    </Grid>

回答1:


First you remove the Statckpanel and put then in a Grid and you can achieve Try something like this.

        <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;
    }

This should give you similar behaviour. Change according to your use



来源:https://stackoverflow.com/questions/28881444/how-control-re-position-depends-on-other-control-visibility-in-same-panel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!