切换按钮上的事件处理程序
Event handler on togglebutton
我正在创建 ToggleButton
,如果我不检查它,它看起来是这样的:
如果我检查它,它看起来像这样:
我使用 MaterialDesign,这是我的代码:
<Grid Grid.Row="0" VerticalAlignment="Center">
<TextBlock Text="Máy in hóa đơn :"
Foreground="White" Margin="15,20,0,0"/>
<ToggleButton VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,20,0,0"/>
</Grid>
如果 ToggleButton
被选中,我想显示 ComboBox
,否则隐藏它。我不知道该怎么做。我不想在后面写代码。
您可以将 x:Name
分配给 ToggleButton
并为 ComboBox
创建一个 Style
,它使用 DataTrigger
更改其 Visibility
取决于 IsChecked
属性 的 ToggleButton
。绑定使用 ElementName
来引用 ToggleButton
.
<StackPanel Orientation="Horizontal">
<TextBlock Text="Máy in hóa đơn :" Foreground="White" Margin="15,20,0,0" />
<ToggleButton x:Name="MyToggleButton" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,20,0,0" />
<ComboBox VerticalAlignment="Top">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=MyToggleButton}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</StackPanel>
或者,使用 Visibility
绑定中的内置 BooleanToVisibilityConverter
。
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</StackPanel.Resources>
<TextBlock Text="Máy in hóa đơn :" Foreground="White" Margin="15,20,0,0" />
<ToggleButton x:Name="MyToggleButton" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,20,0,0" />
<ComboBox VerticalAlignment="Top" Visibility="{Binding IsChecked, ElementName=MyToggleButton, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
我正在创建 ToggleButton
,如果我不检查它,它看起来是这样的:
如果我检查它,它看起来像这样:
我使用 MaterialDesign,这是我的代码:
<Grid Grid.Row="0" VerticalAlignment="Center">
<TextBlock Text="Máy in hóa đơn :"
Foreground="White" Margin="15,20,0,0"/>
<ToggleButton VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,20,0,0"/>
</Grid>
如果 ToggleButton
被选中,我想显示 ComboBox
,否则隐藏它。我不知道该怎么做。我不想在后面写代码。
您可以将 x:Name
分配给 ToggleButton
并为 ComboBox
创建一个 Style
,它使用 DataTrigger
更改其 Visibility
取决于 IsChecked
属性 的 ToggleButton
。绑定使用 ElementName
来引用 ToggleButton
.
<StackPanel Orientation="Horizontal">
<TextBlock Text="Máy in hóa đơn :" Foreground="White" Margin="15,20,0,0" />
<ToggleButton x:Name="MyToggleButton" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,20,0,0" />
<ComboBox VerticalAlignment="Top">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=MyToggleButton}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</StackPanel>
或者,使用 Visibility
绑定中的内置 BooleanToVisibilityConverter
。
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</StackPanel.Resources>
<TextBlock Text="Máy in hóa đơn :" Foreground="White" Margin="15,20,0,0" />
<ToggleButton x:Name="MyToggleButton" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,20,0,0" />
<ComboBox VerticalAlignment="Top" Visibility="{Binding IsChecked, ElementName=MyToggleButton, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>