由于某些原因,下面的代码将不起作用
<ToggleButton Content="Options" x:Name="Options" Height="{Binding ElementName=Connect,Path=ActualHeight}">
<ToggleButton.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="OptionsPanel" Property="Visibility" Value="Collapsed"/>
</Trigger>
</ToggleButton.Triggers>
</ToggleButton>
<StackPanel x:Name="OptionPanel">
</StackPanel>
我得到的错误是
Error 1 The member "IsChecked" is not recognized or is not
accessible.
有人可以帮忙解决我搞砸的事情吗?
我的大脑变成了瑞士奶酪,但我看不到它
您不需要使用ToggleButton.Triggers
,也不需要,因为ControlTemplate
中没有OptionsPanel
。此外,您可能想要使用 Property="ToggleButton.IsChecked"
,但它仍然不适合您。由于您使用的是 x:Name
,因此您可以简单地执行以下操作:
<Page x:Class="WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Page.Resources>
<BooleanToVisibilityConverter x:Key="B2VisibilityConverter" />
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ToggleButton Content="Options"
x:Name="Options" />
<StackPanel Grid.Row="1"
Visibility="{Binding ElementName=Options, Path=IsChecked, Converter={StaticResource B2VisibilityConverter}}">
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
<Button>Button 4</Button>
</StackPanel>
</Grid>
</Page>
单击 ToggleButton
将显示/折叠 StackPanel 及其内容,这正是您想要的方式。