在" ismouseover"上设置一些属性。

时间:2017-12-05 10:11:26

标签: c# wpf

如果鼠标位于Button上,我们会尝试设置一些按钮的属性。 奇怪的是,它只适用于某些属性。

我们创建了一个空白的WPF应用程序,只需添加以下代码行:

<Button Content="{Binding IsMouseOver, RelativeSource={RelativeSource Self},Mode=OneWay}">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                    <Setter Property="Background" Value="Yellow" />
                    <Setter Property="BorderThickness" Value="5"></Setter>
                    <Setter Property="BorderBrush" Value="BurlyWood"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="False">
                <Setter Property="Foreground" Value="Green" />
                <Setter Property="Background" Value="BlueViolet" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>            
</Button>

&#34; false&#34; -Trigger就像魅力一样。 &#34; True&#34; -Trigger仅设置BorderThickness和Foreground。 背景和Borderbrush将被忽略。

谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这是按钮的默认行为,以便更改定义自己的按钮模板,请参阅Control Template

<Button Content="{Binding IsMouseOver, RelativeSource={RelativeSource Self},Mode=OneWay}">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    >                                     
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True">
                        <Setter Property="Foreground" Value="Red" />
                        <Setter Property="Background" Value="Yellow" />
                        <Setter Property="BorderThickness" Value="5"></Setter>
                        <Setter Property="BorderBrush" Value="BurlyWood"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="False">
                        <Setter Property="Foreground" Value="Green" />
                        <Setter Property="Background" Value="BlueViolet" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>