簡體   English   中英

樣式綁定附加屬性未顯示

[英]style binding attached property didn't show up

我正在嘗試設計一個按鈕:當鼠標懸停在按鈕上時,按鈕內容會更改。 因此,我正在嘗試使用附加屬性進行設計。

這是我的附屬課程:

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>

現在,每個按鈕將正常顯示“ Hi”,懸停時顯示“ good”,禁用時顯示“不好”。 但是我的目的是設計一個具有動態內容的按鈕模板。 看來我的綁定效果不好,我也不知道如何解決該問題,請幫忙。 謝謝。

當您綁定到附加屬性(例如Canvas.Left,Grid.Column或自定義屬性)時,可以將它們包裝在括號中。 在您的情況下,請更改對以下內容的綁定

Path=(local:HotButtonAttached.HotStr) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM