简体   繁体   English

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

[英]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. IsMouseOver 转换器颜色和 IsPressed 触发器不起作用。

(I don't want use static resource in my setters) Changing other properties has no effect just changing background made this problem. (我不想在我的 setter 中使用静态资源)更改其他属性没有效果,只是更改背景会导致这个问题。

I found the answer problem was in 2 place:我发现答案问题在 2 个地方:

first one when we use trigger in当我们使用触发器时的第一个

 <ControlTemplate.Triggers/>

you have sure you set your setter set property on the currect object您确定在当前对象上设置了 setter set 属性

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因为MoseOverBrush是父属性而不是 ControlTemplate 属性

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

相关问题 覆盖IsMouseOver并根据WPF按钮中的逻辑更改背景色 - Override IsMouseOver and change background color based on logic in WPF Button WPF IsMouseOver触发器,触发器的命中框不在按钮上方 - WPF IsMouseOver trigger, hitbox for trigger isn't over the button C#WPF MVVM触发器永久更改按钮的背景色 - C# WPF MVVM trigger change background color of button permanently 在代码隐藏中设置按钮的背景后,按钮的触发器 IsMouseOver 停止工作 - Button's trigger IsMouseOver stops working after button's background is set in code-behind WPF中的按钮与isMouseOver以奇怪的方式起作用 - Button in WPF acting in strange way with isMouseOver 使用ismouseover更改对象组的背景颜色 - Change background color on group of objects with ismouseover WPF按钮触发图标更改 - WPF Button trigger icon change WPF样式Tabitem文本在触发时前景,例如IsEnabled,IsMouseOver等 - WPF styling tabitem text foreground upon trigger such as IsEnabled, IsMouseOver, etc 单击后更改wpf C#中按钮的颜色,并在2分钟后保留原始颜色 - change color of button in wpf C# after click and after 2 minutes retain the original color 如何在IsMouseOver触发器上更改UserControl的子元素的属性? - How to change a property of a UserControl's child element on a IsMouseOver trigger?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM