Допустим, у меня есть следующий класс:
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; }
}
Я хочу изменить некоторые свойства пользовательского интерфейса TextBox
через XAML Template Trigger на основе свойства HasFocus
, поэтому я делаю следующее:
<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
я вижу, что триггер зарегистрирован. Если я изменю <Trigger Property="MyClass.HasFocus" Value="True">
на <Trigger Property="MyClass.HasFocus" Value="False">
, мой стиль будет применен изначально. Поэтому я думаю, что мое определение XAML в порядке.
Есть идеи, как решить эту проблему?