c# - XAML 代码 IsChecked 在 ToggleButton 上触发

标签 c# wpf xaml

<分区>

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

<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 及其内容,这正是您想要的方式。

关于c# - XAML 代码 IsChecked 在 ToggleButton 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508479/

相关文章:

c# - 如何从 WPF 中通过 SaveFileDialog 保存 BitmapImage?

c# - 面试题: . Any() vs if (.Length > 0) 测试集合是否有元素

c# - 是否可以为 WPF 和 Silverlight 编译相同的控件?

c# - 使用 ASP.Net C#、DropDownList 和重定向?

wpf - MVVM 和服务对象

c# - 如何在 wpf 的分层数据模板中显示 TreeView 项的上下文菜单

c# - 组合框文本属性双向绑定(bind)不起作用

c# - Windows Phone 8 滚动文本框的内容

c# - 设置网格以填充整个屏幕

c# - 命名空间困惑