样式绑定附加属性没有显示

时间:2015-12-19 17:51:31

标签: c# wpf attached-properties

我正在尝试设计一个按钮:当鼠标悬停在按钮上时,按钮内容会发生变化。所以我试图使用附加属性来设计它。

这是我附上的课程:

class HotButtonAttached:DependencyObject
{
    public static int GetNormalStr(DependencyObject obj)
    {
        return (int)obj.GetValue(NormalStrProperty);
    }

    public static void SetNormalStr(DependencyObject obj, int value)
    {
        obj.SetValue(NormalStrProperty, value);
    }

    // Using a DependencyProperty as the backing store for NormalStr.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NormalStrProperty =
        DependencyProperty.RegisterAttached("NormalStr", typeof(int), typeof(HotButtonAttached), new UIPropertyMetadata(0));

    public static int GetHotStr(DependencyObject obj)
    {
        return (int)obj.GetValue(HotStrProperty);
    }

    public static void SetHotStr(DependencyObject obj, int value)
    {
        obj.SetValue(HotStrProperty, value);
    }

    // Using a DependencyProperty as the backing store for HotStr.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HotStrProperty =
        DependencyProperty.RegisterAttached("HotStr", typeof(int), typeof(HotButtonAttached), new UIPropertyMetadata(0));


    public static int GetDisable(DependencyObject obj)
    {
        return (int)obj.GetValue(DisableProperty);
    }

    public static void SetDisable(DependencyObject obj, int value)
    {
        obj.SetValue(DisableProperty, value);
    }

    // Using a DependencyProperty as the backing store for Disable.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DisableProperty =
        DependencyProperty.RegisterAttached("Disable", typeof(int), typeof(HotButtonAttached), new PropertyMetadata(0));

}

我为它设计了一种风格:

    <Style x:Key="btnStyle1" TargetType="Button">
        <Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.NormalStr, RelativeSource={RelativeSource Self}}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.HotStr,RelativeSource={RelativeSource Self}}"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.Disable,RelativeSource={RelativeSource Self}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

我将样式应用于:

    <Button  x:Name="Btn" Click="Button_Click" Width="48" Height="48" Margin="5" Style="{DynamicResource btnStyle1}"
             local:HotButtonAttached.NormalStr="11111"
             local:HotButtonAttached.HotStr="22222"
             local:HotButtonAttached.Disable="3333">
    </Button>

但问题是按钮没有显示任何内容(悬停时应显示11111,22222,禁用时显示3333)。

如果我将样式更改为:

     <Style x:Key="btnStyle1" TargetType="Button">
        <Setter Property="Content" Value="Hi"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Content" Value="Good"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Content" Value="Not good"/>
            </Trigger>
        </Style.Triggers>
    </Style>

现在每个按钮都会出现&#34;嗨&#34;像往常一样,&#34;好&#34;当悬停时,&#34;不好&#34;禁用时。但我的目标是设计一个包含动态内容的按钮模板。看来我的绑定效果不好,我不知道如何解决,请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

当您绑定到附加属性(如Canvas.Left,Grid.Column或自定义属性)时,将它们包装在括号中。在你的情况下改变绑定到以下

Path=(local:HotButtonAttached.HotStr)