更改后wpf按钮IsMouseOver触发颜色不起作用

时间:2018-02-14 21:43:58

标签: c# wpf

这是我的按钮代码

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

}

我将这个样式用于我的按钮

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

但问题出在那里,当我为我的按钮背景添加颜色时,如下面的代码:

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

IsMouseOver更换器颜色和IsPressed触发器不起作用。

(我不想在我的setter中使用静态资源) 改变其他属性只会改变背景造成这个问题。

1 个答案:

答案 0 :(得分:0)

我发现答案问题在两个地方:

我们在

中使用触发器时的第一个
 <ControlTemplate.Triggers/>

您确定在当前对象

上设置了setter set属性

并且secend one在我们已经改变的绑定中

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

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

因为MoseOverBrush是parrent属性而不是ControlTemplate属性