本文介绍了XAML 代码 IsChecked 在 ToggleButton 上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,下面的代码不起作用

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>

我得到的错误是

错误 1 ​​成员IsChecked"未被识别或未被识别可访问.

有人可以帮忙解决我搞砸的事情吗?我的大脑已经转向瑞士奶酪,我看不到它

Could someone please assist in what I screwed up?My brain has turned to Swiss cheese and I cant see it

推荐答案

你不需要使用 ToggleButton.Triggers,你也不能因为没有 OptionsPanelControlTemplate 中.此外,您可能想使用 Property="ToggleButton.IsChecked",但它仍然不适合您.由于您使用的是 x:Name,您可以简单地执行以下操作:

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>

单击 ToggleButton 将按照您想要的方式显示/折叠 StackPanel 及其内容.

Clicking the ToggleButton will show/collapse the StackPanel and it's contents just the way you want.

这篇关于XAML 代码 IsChecked 在 ToggleButton 上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:37