成员“ IsChecked”未被识别或无法访问

时间:2020-01-02 11:15:12

标签: c# wpf .net-core visual-studio-2019 .net-core-3.1

我正在使用 .NetCore 3.1

在Visual Studio 2019中创建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>

它显示消息

成员IsChecked无法识别或无法访问。

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

2 个答案:

答案 0 :(得分:0)

被咬, 您可以尝试以下代码:

    <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>

谢谢

答案 1 :(得分:0)

对于其他任何人,由于ControlTemplate没有任何TargetType,因此无法访问IsChecked属性。

对我有用的是-

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