Reputation: 315
Actually the following answer does not solve my purpose. Hence raising another question. How control re-position depends on other control visibility in same panel. I have two buttons inside a stack panel. Initially B1 button is on top, then B2.
<Grid>
<StackPanel >
<Button Content="B1" Height="20" Width="100" Visibility="Visible"/>
<Button Content="B2" Height="20" Width="100" Visibility="Visible"/>
</StackPanel>
</Grid>
So, using this XAML, when I open application, I can see both B1 and B2 button in UI. Now, pragmatically, if I do B1's visibility Hidden, then B2 will take the position of B1 button, and again if I do B1's visibility "Visible", then in the UI, B1 & B2 both will be shown. How to achieve this functionality?
Upvotes: 0
Views: 75
Reputation: 5366
You can do it using Visibility.Collapsed of the button.
<StackPanel>
<StackPanel >
<Button Content="B1" Height="20" Width="100" Visibility="Visible" x:Name="B1"/>
<Button Content="B2" Height="20" Width="100" Visibility="Visible" x:Name="B2"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="Collapse B1" Click="Button_Click"/>
<Button Content="Collapse B2" Click="Button_Click_1"/>
<Button Content="Visible Both" Click="Button_Click_2"/>
</StackPanel>
</StackPanel>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
B1.Visibility = Visibility.Collapsed;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
B2.Visibility = Visibility.Collapsed;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
B1.Visibility = Visibility.Visible;
B2.Visibility = Visibility.Visible;
}
}
Upvotes: 1