成员“IsChecked”未被识别或不可访问

The member “IsChecked” is not recognized or is not accessible

我正在 Visual studio 2019 年使用 .NetCore 3.1

创建 WPF 桌面应用程序
<Style TargetType="{x:Type ToggleButton}"
       x:Key="toggle">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <ControlTemplate.Triggers>
          <Trigger Property="IsChecked"
                   Value="True">
            <Setter Property="Background"
                    Value="Beige" />
          </Trigger>
          <Trigger Property="IsChecked"
                   Value="False">
            <Setter Property="Background"
                    Value="Brown" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<Grid>
    <ToggleButton Style="{DynamicResource toggle}" Width="150" Height="150" Background="Aquamarine"/>
</Grid>

所以它显示消息

The member IsChecked is not recognized or is not accessible.

任何人都知道我的这段代码可能做错了什么。谢谢

下巴, 您可以尝试使用以下代码:

    <Window.Resources>
        <Style TargetType ="{x:Type ToggleButton}" x:Key="toggle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">  
                        <Border CornerRadius="3" Background="{TemplateBinding Background}">
                            <ContentPresenter Margin="3" 
               HorizontalAlignment="Center" 
               VerticalAlignment="Center"/>
                        </Border>                      
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <SolidColorBrush>                                            <SolidColorBrush.Color>Beige</SolidColorBrush.Color>
                                        </SolidColorBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <SolidColorBrush>
                                            <SolidColorBrush.Color>Brown</SolidColorBrush.Color>
                                        </SolidColorBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>            
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <ToggleButton Style="{DynamicResource toggle}" Width="150" Height="150"/>
    </Grid>

谢谢,

对于其他人,属性 IsChecked 不可访问,因为 ControlTemplate 没有任何 TargetType。

对我有用的是-

<ControlTemplate TargetType={x:Type ToggleButton}>
    <ControlTemplate.Triggers>
        ...
    </ControlTemplate.Triggers>
</ControlTemplate>