style binding attached property didn't show up

半城伤御伤魂 提交于 2019-12-13 07:38:07

问题


I am trying to design a button: When the mouse hover on the button, the button content changes. So I am trying to use attached property to design it.

Here is my attached class:

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

}

And I design a style for it:

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

And I apply the style with:

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

But the problem is the button didn't show up any contents(it should show up 11111, 22222 when hover,3333 when disable ).

If I change the style to:

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

Now that every button will show up "Hi" as normal, and "good" when hover, and "not good" when disable. But my aim is to design a button template with dynamic contents. It seems my binding did not work well, and I have no idea of how to solve that, please help. Thanks.


回答1:


When you bind to attached properties such as Canvas.Left, Grid.Column or custom ones, then you wrap them in parenthesis. In your case alter binding to following

Path=(local:HotButtonAttached.HotStr) 


来源:https://stackoverflow.com/questions/34373686/style-binding-attached-property-didnt-show-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!