WPF toggle panel visibility
I have two panel开发者_运维技巧s, only one should be visible at the same time. I change between them by clicking a button, one on each panel.
Is there a nice way to do this in xaml without codebehind or viewmodel?
It's actually possible, however quite tricky.
My example works without any code-behind, actually without any value converters, too.
Here is the code: (now simplified version, thanks to @H.B. for ideas)
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="RBToggleButtonStyle" TargetType="RadioButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ToggleButton
IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="25"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="15"/>
<Setter Property="Content" Value="Hide"/>
</Style>
<Style x:Key="MyBorderStyle" TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Background="Green" Grid.Column="0" Style="{StaticResource MyBorderStyle}" DataContext="{Binding ElementName=greenB}">
<RadioButton x:Name="greenB" GroupName="x" Style="{StaticResource RBToggleButtonStyle}"/>
</Border>
<Border Background="Red" Grid.Column="1" Style="{StaticResource MyBorderStyle}" DataContext="{Binding ElementName=redB}">
<RadioButton x:Name="redB" GroupName="x" Style="{StaticResource RBToggleButtonStyle}"/>
</Border>
</Grid>
</Page>
The idea of using ToggleButtons
is stolen from some other question at SO
The suggestion with using a tabcontrol is good. I found some code which styles a TabControl to only show the TabItem content
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TabControl BorderThickness="0" Padding="0">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Header="Not shown">
<Grid Background="Red"/>
</TabItem>
<TabItem>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Tab 2
</TextBlock>
</TabItem>
</TabControl>
</Grid>
</P
If you used ToggleButtons, then you could bind the visibility of Panel 1 to the IsChecked state of Button 2, and the visibility of Panel 2 to the IsChecked state of Button 1. Make them TwoWay bindings and use the built-in BooleanToVisibility converter.
Why not use the TabControl for this?
A little work around with a IValueConverter
should do the trick. Sure it's not plain old XAML, but it's not in the codebehind and can be reuse.
I see something like binding X visibility to Y visibility and add a Converter to it :
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (Visibility)value == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
I don't think so. You would need to use viewmodel or codebehind. Use a Style with a DataTrigger and bind the value of the visibility property to a property in viewmodel, avoiding the use of codebehind.
精彩评论