WPF触发器绑定不起作用

时间:2016-09-15 09:44:21

标签: wpf vb.net

我已阅读了几篇处理相同问题的帖子,但我找不到解决方案。

我想要实现的是,WPF复选框在鼠标悬停时更改其背景和边框刷颜色。颜色不应该在XAML中进行硬编码。应将背景和borderbrush属性绑定到POCO类中的属性。

这就是我所做的:

首先,我创建了一个派生自WPF CheckBox的POCO类。我创建了一个名为MouseOverBorderBrush的依赖项属性:

Public Class efCheckBox
    Inherits CheckBox
    Implements INotifyPropertyChanged

    Public Shared ReadOnly MouseOverBorderBrushProperty As DependencyProperty =
   DependencyProperty.Register("MouseOverBorderBrush",
   GetType(Brush), GetType(efCheckBox))

Public Property MouseOverBorderBrush As Brush
    Get
        Return CType(GetValue(MouseOverBorderBrushProperty), Brush)
    End Get

    Set(ByVal value As Brush)
        PropertyHasChanged(MouseOverBorderBrushProperty, value)
    End Set
End Property

 Private Sub PropertyHasChanged(dp As DependencyProperty, value As Object, <System.Runtime.CompilerServices.CallerMemberName> Optional p As String = Nothing)
    SetValue(dp, value)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(p))
End Sub

其次,我创建了一个名为&#34; efComboBox&#34;的ResourceDictionary。我通过克隆原始WPF CheckBox的模板来做到这一点,所以下面的代码是 - 除了两个例外 - 原始的CheckBox定义:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:emmbeeFramework"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">

<SolidColorBrush x:Key="CheckBoxFillNormal" Color="#F4F4F4"/>
<SolidColorBrush x:Key="CheckBoxStroke" Color="#8E8F8F"/>
<Style x:Key="EmptyCheckBoxFocusVisual" TargetType="{x:Type local:efCheckBox}">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="1" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="CheckRadioFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="efCheckBoxStyle" TargetType="{x:Type local:efCheckBox}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="{StaticResource CheckBoxFillNormal}"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyCheckBoxFocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:efCheckBox}">
                <BulletDecorator x:Name="bullet" Background="Transparent" SnapsToDevicePixels="true">
                    <BulletDecorator.Bullet>
                        <Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderPressed="{TemplateBinding IsPressed}"/>
                    </BulletDecorator.Bullet>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasContent" Value="true">
                        <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                        <Setter Property="Padding" Value="4,0,0,0"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" Value="{Binding local:efCheckBox.MouseOverBorderBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

两个例外:

  1. 我吟唱了#34; TargetType&#34;来自&#34; CheckBox&#34;到&#34; efCheckBox&#34;
  2. 我添加了一个触发器&#34; IsMouseOver&#34;接近XAML代码的末尾
  3. 此绑定无效:

    <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="BorderBrush" Value="{Binding local:efCheckBox.MouseOverBorderBrush}"/>
    </Trigger>
    

    我也尝试过,但它不起作用:

    <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:efCheckBox}}, Path=MouseOverBorderBrush}"/>
    

    如果我对画笔进行硬编码,则可以正常工作:

    <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="BorderBrush" Value="Red"/>
    </Trigger>
    
    
    <Setter Property="BorderBrush" Value="Red">
    

    有人可以帮助我吗?

0 个答案:

没有答案