猿问

如何使用 XAML 样式模板绑定到另一个对象属性?

假设我有以下课程:


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; }

}

我想根据 property 更改TextBoxvia XAML Template Trigger 的一些 UI 属性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 定义没问题。


任何想法如何解决这个问题?


繁星淼淼
浏览 144回答 1
1回答

侃侃尔雅

应用于 a 的模板中的元素TextBox不能绑定到 a 的属性MyClass,除非有一个MyClass元素可以绑定到可视化树中的某处。如果您希望能够设置 a 的自定义HasFocus属性TextBox,您应该创建一个附加属性:public class FocusExtensions{&nbsp; &nbsp; public static readonly DependencyProperty SetHasFocusProperty = DependencyProperty.RegisterAttached(&nbsp; &nbsp; &nbsp; &nbsp; "HasFocus",&nbsp; &nbsp; &nbsp; &nbsp; typeof(bool),&nbsp; &nbsp; &nbsp; &nbsp; typeof(FocusExtensions),&nbsp; &nbsp; &nbsp; &nbsp; new FrameworkPropertyMetadata(false)&nbsp; &nbsp; );&nbsp; &nbsp; public static void SetHasFocus(TextBox element, bool value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; element.SetValue(SetHasFocusProperty, value);&nbsp; &nbsp; }&nbsp; &nbsp; public static bool GetHasFocus(TextBox element)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return (bool)element.GetValue(SetHasFocusProperty);&nbsp; &nbsp; }}它可以为任何TextBox元素设置:<TextBox local:FocusExtensions.HasFocus="True">&nbsp; &nbsp; <TextBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="{x:Type TextBox}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="local:FocusExtensions.HasFocus" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderBrush" Value="Red" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderThickness" Value="2" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; </TextBox.Style></TextBox>
随时随地看视频慕课网APP
我要回答