Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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# 如何绑定到另一个';具有XAML样式模板的对象属性?_C#_Wpf_Dependency Properties - Fatal编程技术网

C# 如何绑定到另一个';具有XAML样式模板的对象属性?

C# 如何绑定到另一个';具有XAML样式模板的对象属性?,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,假设我有以下课程: public class MyClass : System.Windows.FrameworkElement { public static readonly DependencyProperty HasFocusProperty = DependencyProperty.RegisterAttached("HasFocus", typeof(bool), typeof(MyClass), new PropertyMetadata(default(bool)));

假设我有以下课程:

public class MyClass : System.Windows.FrameworkElement
{
    public static readonly DependencyProperty HasFocusProperty = DependencyProperty.RegisterAttached("HasFocus", typeof(bool), typeof(MyClass), new PropertyMetadata(default(bool)));

    public bool HasFocus
    {
        get => (bool)GetValue(HasFocusProperty);
        set => SetValue(HasFocusProperty, value);
    }

    public System.Windows.Controls.TextBox TextBox { get; set; }
}
我想基于属性
HasFocus
通过XAML模板触发器更改
TextBox
的一些UI属性,因此我执行以下操作:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:win="clr-namespace:System.Windows.Controls">
    <Style TargetType="{x:Type win:TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type win:TextBox}">
                    <ControlTemplate.Triggers>
                        <Trigger Property="MyClass.HasFocus" Value="True">
                            <Setter TargetName="Border" Property="BorderBrush" Value="Red" />
                            <Setter TargetName="Border" Property="BorderThickness" Value="2" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
 </ResourceDictionary>

但是,设置
HasFocus=true
时不应用该样式

TextBox
的属性中,我可以看到触发器已注册。如果我将
更改为
,则最初会应用我的样式。所以我认为我的XAML定义是正确的


有没有办法解决这个问题?

模板中应用于
文本框的元素不能绑定到
MyClass
的属性,除非在可视化树中有一个
MyClass
元素要绑定到

如果希望能够设置
文本框
的自定义
HasFocus
属性,则应创建:

可以为任何
文本框
元素设置:

<TextBox local:FocusExtensions.HasFocus="True">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="local:FocusExtensions.HasFocus" Value="True">
                    <Setter Property="BorderBrush" Value="Red" />
                    <Setter Property="BorderThickness" Value="2" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

模板中应用于
文本框的元素不能绑定到
MyClass
的属性,除非在可视化树中的某个位置有
MyClass
元素要绑定

如果希望能够设置
文本框
的自定义
HasFocus
属性,则应创建:

可以为任何
文本框
元素设置:

<TextBox local:FocusExtensions.HasFocus="True">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="local:FocusExtensions.HasFocus" Value="True">
                    <Setter Property="BorderBrush" Value="Red" />
                    <Setter Property="BorderThickness" Value="2" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>


您正在为内置的
文本框创建模板,并尝试绑定到
MyClass
的属性。这没有道理。
TextBox
应该如何在那里查找
MyClass
HasFocus
属性?也许你想要一个。或者
MyClass
的目的是什么?我是WPF的新手,请原谅我的问题:为什么它没有意义?我希望
TextBox
对另一个类的属性更改做出反应。为此,我是否必须使用附加属性而不是依赖属性?依赖项属性是否仅支持同一类内的更改?是的,针对类型a的模板无法绑定到另一类型B的属性,除非有此类型的实例要绑定到somwhere。感谢您的解释!我可以绑定到
MyClass
TextEditor
实例吗?对不起,我是指
MyClass
TextBox
实例,您正在为内置
TextBox
创建模板,并尝试绑定到
MyClass
的属性。这没有道理。
TextBox
应该如何在那里查找
MyClass
HasFocus
属性?也许你想要一个。或者
MyClass
的目的是什么?我是WPF的新手,请原谅我的问题:为什么它没有意义?我希望
TextBox
对另一个类的属性更改做出反应。为此,我是否必须使用附加属性而不是依赖属性?依赖项属性是否仅支持同一类内的更改?是的,针对类型a的模板无法绑定到另一类型B的属性,除非有此类型的实例要绑定到somwhere。感谢您的解释!我可以绑定到
MyClass
TextEditor
实例吗?对不起,我是指
MyClass
TextBox
实例