简体   繁体   中英

Change Property by a Behavior disable Trigger in XAML Style

Below you can se a part of my Behavior and XAML Style. So if no Behavior is attached, everything is fine. But with the attached Behavior the Trigger is not fired anymore and I only have this new Color on my background. Someone can give me a tip, how to change the Color without overriding the Trigger ?

Behavior Snippet

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 Snippet

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

Try to make DependencyProperty like this.

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

where SolidColorBrush(Colors.Black) is default color, and this set it to bind TwoWay by default FrameworkPropertyMetadataOptions.BindsTwoWayByDefault) ,maybe that was a problem, becouse if you dont set it there to bind TwoWay by default then you have to make that in binding Mode=TwoWay, or else your Property wont register that change. Hope that will help you.¸

EDIT: As i can see you are using Button as base class and want to change button background on trigger? I really can't say what is the best way to do it, but i would do it in generic.xaml ,a and in <ControlTemplate.Triggers> i would do it like this:

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

        </Trigger>

</ControlTemplate.Triggers>

And Up in beggining of your style you have something like this:

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

just change that line of code to this:

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

That way you have set your Default color in the begining, and your NewColor on Trigger.

and when you call that custom button in some xaml code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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