问题
For some reason the following code will not work
<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>
the error I am getting is
Error 1 The member "IsChecked" is not recognized or is not accessible.
Could someone please assist in what I screwed up? My brain has turned to Swiss cheese and I cant see it
回答1:
You don't need to use ToggleButton.Triggers
, nor can you because there is no OptionsPanel
in the ControlTemplate
. Additionally, you'd want to use Property="ToggleButton.IsChecked"
, but it still wouldn't work out for you. Since you are using x:Name
, you can simply do this:
<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>
Clicking the ToggleButton
will show/collapse the StackPanel and it's contents just the way you want.
来源:https://stackoverflow.com/questions/25508479/xaml-code-ischecked-trigger-on-togglebutton