XAML Code IsChecked Trigger On ToggleButton [duplicate]

做~自己de王妃 提交于 2020-02-24 12:57:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!