ToggleButton IsChecked触发器 [英] ToggleButton IsChecked trigger
问题描述
我正在编写一个应用程序,其中有一个 ToggleButton
和其他UI控件。当 ToggleButton
的状态为 IsChecked
时,我想实现以下两点。
I am writing one application where I have a ToggleButton
and other few controls on UI. I want to achieve bellow two things when state of ToggleButton
is IsChecked
.
1) Hide few controls e.g. button1,button2 etc. in below example
2) Change 'Content' of ToggleButton to 'Show'
我可以实现No 2,但不知道如何访问其他控件并在 Trigger
中设置其属性。我的xaml代码如下所示:
I can achieve point no 2 but don't know how to access other control and set their properties inside a Trigger
. My xaml code is shown below:
<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 ',当切换按钮的状态更改为取消选中时,再次显示button1。
Question: When user clicks on toggle button 'tg' I want to hide 'button1','button2' and when state of toggle button changes to un-check again show the button1.
如何使用 ToggleButton
的 IsChecked
属性?
推荐答案
在您的资源声明 BoolToVisibilityConverter
如下:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</Window.Resources>
然后将button1的可见性设置为绑定:
Then set the visibility of button1 as binding:
<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}}"/>
这篇关于ToggleButton IsChecked触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!