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