简体   繁体   中英

After change wpf button IsMouseOver trigger color not work

this is my button code

 public class MetroButton : Button
{
    public static readonly DependencyProperty MoseOverBrushProperty;
    public static readonly DependencyProperty PressedBrushProperty;

    public MetroButton():base()
    {

        var resource = new ResourceDictionary
        {
            Source = new Uri("/Parking.Component.Ui;component/Styles/ButtonMetro.xaml",
                     UriKind.RelativeOrAbsolute)
        };
        Style = resource["ButtonMetro"] as Style;
        //SetResourceReference(StyleProperty, Style);
    }
    static MetroButton()
    {

        MoseOverBrushProperty = DependencyProperty.Register("MoseOverBrush", typeof(Brush), typeof(MetroButton));
        PressedBrushProperty = DependencyProperty.Register("PressedBrush", typeof(Brush), typeof(MetroButton));
    }


    public Brush MoseOverBrush
    {
        get { return (Brush)base.GetValue(MoseOverBrushProperty); }
        set { base.SetValue(MoseOverBrushProperty, value); }            
    }
    public Brush PressedBrush
    {
        get { return (Brush)base.GetValue(PressedBrushProperty); }
        set { base.SetValue(PressedBrushProperty, value); }
    }

}

and I use this style for my button

<Style   x:Key="ButtonMetro" TargetType="{ x:Type LochalUI:MetroButton}">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="MoseOverBrush" Value="#FF3F62FD"/>
    <Setter Property="PressedBrush" Value="#FF000099"/>
    <Setter Property="Background" Value="#FF6B9AFF"/>
    <Setter Property="FontSize" Value="15" />
    <Setter Property="FontFamily" Value="B Yekan" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type LochalUI:MetroButton}">
                <Border x:Name="border" CornerRadius="4" Background="{TemplateBinding Background}">
                    <Grid>
                        <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{Binding Path=MoseOverBrush , RelativeSource={RelativeSource Self}}" />
                        <Setter Property="Foreground" Value="Black" />

                    </Trigger>

                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="{Binding Path=PressedBrush , RelativeSource={RelativeSource Self}}" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

but the problem is there when i put color for my button background like below code:

 <UI:MetroButton HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="132" Height="107" Background="#FF09CD00"  >
        <Grid>
            <Label Content="تنظیمات" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="5,5,5,12" Foreground="White" Margin="5"/>
        </Grid>
    </UI:MetroButton>

the IsMouseOver changer color and IsPressed Triggers not work.

(I don't want use static resource in my setters) Changing other properties has no effect just changing background made this problem.

I found the answer problem was in 2 place:

first one when we use trigger in

 <ControlTemplate.Triggers/>

you have sure you set your setter set property on the currect object

and the secend one is in the binding we have change

Value="{Binding Path=MoseOverBrush , RelativeSource={RelativeSource Self}}"

to

Value="{Binding Path=MoseOverBrush , RelativeSource={RelativeSource TemplatedParent}}" 

because MoseOverBrush is parrent property not the ControlTemplate property

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