繁体   English   中英

通过行为更改属性禁用XAML样式中的触发器

[英]Change Property by a Behavior disable Trigger in XAML Style

您可以在下面查看我的Behavior和XAML样式的一部分。 因此,如果没有附加行为,一切都很好。 但是随着附加的BehaviorTrigger不再被Trigger ,我的背景上只有这个新颜色。 有人可以给我一个提示,如何更改颜色而不覆盖Trigger

行为代码段

public class ChangeColor : Behavior<FrameworkElement>
{
    public string NewColor
    {
        get { return (string)GetValue(NewColorProperty); }
        set { SetValue(NewColorProperty, value); }
    }

    public static DependencyProperty NewColorProperty = DependencyProperty.Register("NewColor", typeof(string), typeof(ChangeColor), new PropertyMetadata(""));

    protected override void OnAttached()
    {
        if (!DesignerProperties.GetIsInDesignMode(this))
        {
            AssociatedObject.Loaded += AssociatedObject_Loaded;
        }
    }
    public Brush DefaultColor
    {
        get { return (Brush)GetValue(DefaultColorProperty); }
        set { SetValue(DefaultColorProperty, value); }
    }

    public static DependencyProperty DefaultColorProperty = DependencyProperty.Register("DefaultColor", typeof(Brush), typeof(ChangeColor), null);

    private PropertyInfo _TargetProperty;

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        _TargetProperty = AssociatedObject.GetType().GetProperty("Background");

        if (DefaultColor == null)
        {
            try
            {
                DefaultColor = (Brush)_TargetProperty.GetGetMethod().Invoke(AssociatedObject, null);
            }
            catch
            {
                //ignore
            }
        }
    }

    private void ChangeTheColor()
    {
        if ((bool)change)
        {
            _TargetProperty.GetSetMethod().Invoke(AssociatedObject, new object[] { NewColor });
        }
        else
        {
            _TargetProperty.GetSetMethod().Invoke(AssociatedObject, new object[] { DefaultColor });
        }
    }
}

XAML片段

<Style TargetType="Controls:CustomButton">
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsReadOnly" Value="False"/>
                <Condition Property="IsMouseOver" Value="True"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Background" Value="{Extensions:ThemeColors KeyCode=BackgroundSpecialColor}"/>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

尝试像这样制作DependencyProperty。

       public Brush NewColor
        {
            get { return (Brush)GetValue(NewColorProperty); }
            set { SetValue(NewColorProperty , value); }
        }

        public static readonly DependencyProperty NewColorProperty =
            DependencyProperty.Register("NewColor" , typeof(Brush) , 
                typeof(ChangeColor) ,
                  new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Black) ,
                       FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

其中SolidColorBrush(Colors.Black)是默认颜色,这将其设置为默认绑定TwoWay FrameworkPropertyMetadataOptions.BindsTwoWayByDefault) ,也许这是一个问题,因为如果你没有设置它默认绑定TwoWay然后你必须在绑定Mode = TwoWay,否则你的Property不会注册该更改。 希望对你有所帮助

编辑:因为我可以看到你使用Button作为基类,并希望更改触发器上的按钮背景? 我真的不能说最好的方法是什么,但我会在generic.xaml ,a和<ControlTemplate.Triggers>这样做我会这样做:

<ControlTemplate.Triggers>
      <Trigger Property="IsPressed"
               Value="true">
         <Setter Property="Background"                                   
                 Value="{Binding NewColor, RelativeSource={RelativeSource TemplatedParent}}" />

        </Trigger>

</ControlTemplate.Triggers>

在你的风格开始,你有这样的事情:

<Setter Property="Background"
                Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />

只需将该行代码更改为:

<Setter Property="Background"
                    Value="{Binding DefaultColor, RelativeSource={RelativeSource TemplatedParent}}" />

这样你就可以在开始时设置默认颜色,在触发器上设置NewColor。

当你在某些xaml代码中调用该自定义按钮时

<Grid>
    <customButton:Button DefaultColor="set your default color"
                         NewColor="Set your onClick color".../>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM