Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Wpf 附加属性上的样式触发器_Wpf_Xaml - Fatal编程技术网

Wpf 附加属性上的样式触发器

Wpf 附加属性上的样式触发器,wpf,xaml,Wpf,Xaml,我创建了自己的附加属性,如下所示: public static class LabelExtension { public static bool GetSelectable(DependencyObject obj) { return (bool)obj.GetValue(SelectableProperty); } public static void SetSelectable(Depende

我创建了自己的附加属性,如下所示:

 public static class LabelExtension
    {
        public static bool GetSelectable(DependencyObject obj)
        {
            return (bool)obj.GetValue(SelectableProperty);
        }
        public static void SetSelectable(DependencyObject obj, bool value)
        {
            obj.SetValue(SelectableProperty, value);
        }
        // Using a DependencyProperty as the backing store for Selectable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectableProperty =
            DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(Label), new UIPropertyMetadata(false));
    }
然后我尝试创建一个带有触发器的样式,该触发器依赖于此:

<!--Label-->
<Style TargetType="{x:Type Label}">
    <Style.Triggers>
        <Trigger Property="Util:LabelExtension.Selectable" Value="True">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <TextBox IsReadOnly="True" Text="{TemplateBinding Content}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

如何在样式触发器中访问附加属性的值?我尝试过使用DataTrigger和RelativeSource绑定,但它无法实现值。

您的触发器声明很好,但附加的属性声明有一个小问题。依赖项属性的所有者类型必须是声明该属性的类型,而不是计划将其附加到的类型。因此:

DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(Label)...
需要对此进行更改:

DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(LabelExtension)...
                                                                ^^^^^^^^^^^^^^^^^^^^^^

谢谢,我发现将所有者类型更改为object解决了这个问题,但我不明白为什么。
DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(LabelExtension)...
                                                                ^^^^^^^^^^^^^^^^^^^^^^