我正在编写一个应用程序,其中我在UI上有ToggleButton
和其他几个控件。当ToggleButton
的状态为IsChecked
时,我想要实现两件事。
1) Hide few controls e.g. button1,button2 etc. in below example 2) Change 'Content' of ToggleButton to 'Show'
我可以达到第2点,但不知道如何访问其他控件并在Trigger
内设置其属性。我的xaml代码如下所示:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="108*" />
<RowDefinition Height="107*" />
<RowDefinition Height="96*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209*" />
<ColumnDefinition Width="161*" />
<ColumnDefinition Width="133*" />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="tg"
Height="20"
Width="80"
Grid.Column="1"
Margin="2,38,79,49"
Grid.Row="1">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Content"
Value="Hide" />
<Style.Triggers>
<Trigger Property="IsChecked"
Value="true">
<Setter Property="{Binding ElementName=Button1,Path=Content}"
Value="Show" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
<Button Content="Button1"
Height="23"
HorizontalAlignment="Left"
Margin="45,28,0,0"
Name="button1"
VerticalAlignment="Top"
Width="75" />
<Button Content="Button2"
Height="23"
HorizontalAlignment="Left"
Margin="38,28,0,0"
Name="button2"
VerticalAlignment="Top"
Width="75"
Grid.Column="1" />
</Grid>
问题:当用户点击切换按钮'tg'时,我想隐藏'button1','button2',当切换按钮状态改变为取消检查时再显示按钮1。
如何使用ToggleButton
的{{1}}属性切换button1的可见性?
答案 0 :(得分:1)
在您的资源中声明BoolToVisibilityConverter
,如下所示:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</Window.Resources>
然后将button1的可见性设置为绑定:
<Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="45,28,0,0" Name="button1" VerticalAlignment="Top" Width="75"
Visibility="{Binding ElementName=tg, Path=IsChecked, Converter={StaticResource BoolToVis}}"/>