Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将样式触发器属性设置为自定义依赖项属性_C#_Wpf_Xaml_Triggers_Dependency Properties - Fatal编程技术网

C# 无法将样式触发器属性设置为自定义依赖项属性

C# 无法将样式触发器属性设置为自定义依赖项属性,c#,wpf,xaml,triggers,dependency-properties,C#,Wpf,Xaml,Triggers,Dependency Properties,在使用usercontrol的xaml文件时,我遇到了另一个问题-。-' 我试图为我的自定义按钮实现一个IsChecked属性,以便在选中按钮时设置不同的背景色 因此,我创建了一个DependencyProperty,如下所示: public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(LeftMenuBtn

在使用usercontrol的xaml文件时,我遇到了另一个问题-。-'

我试图为我的自定义按钮实现一个
IsChecked
属性,以便在选中按钮时设置不同的背景色

因此,我创建了一个
DependencyProperty
,如下所示:

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(LeftMenuBtn));

public bool IsChecked
{
    get { return (bool)GetValue(IsCheckedProperty); }
    set { SetValue(IsCheckedProperty, value); }
}
然后我设置了一个新的样式触发器来处理此属性:

<Style x:Key="ButtonEnableStates" TargetType="{x:Type Grid}">
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Background" Value="{DynamicResource CheckedStateGradient}" />
        </Trigger>
    </Style.Triggers>
</Style>

Expression Blend现在在
Property=“IsChecked”
下面加下划线,并说:

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


如何解决此问题?

嗯,
样式的
目标类型是
网格
,属性是为
LeftMenuBtn
定义的,不会这样工作。

谢谢您的回答。因此,我尝试将
typeof(LeftMenuBtn)
更改为
typeof(Grid)
,但这也不起作用?@TorbenJonas:您不能像这样向现有类添加属性,但是您可以注册一个for
LeftMenuBtn
,并在触发器中使用
local:LeftMenuBtn.IsChecked
。好的,感谢链接尝试此操作。很抱歉问了这么多愚蠢的问题,但是这个触发器和属性的话题现在让我有点困惑^^