簡體   English   中英

UWP Setter自定義DependencyProperty

[英]UWP Setter custom DependencyProperty

我有一個名為IconTextButton ,它定義了一些屬性。

public sealed partial class IconTextButton : UserControl
{
    public string Label
    {
        get => (string)GetValue(LabelProperty);
        set => SetValue(LabelProperty, value);
    }
    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label",
                                                                                          typeof(string),
                                                                                          typeof(IconTextButton),
                                                                                          new PropertyMetadata(""));
    public IconElement Icon { get; set; }
    public IconTextButtonLabelPosition LabelPosition
    {
        get => (IconTextButtonLabelPosition)GetValue(LabelPositionProperty);
        set
        {
            SetValue(LabelPositionProperty, value);
            Grid.SetColumn(LabelTextBlock, value == IconTextButtonLabelPosition.Left ? 0 : 2);
        }
    }
    public static readonly DependencyProperty LabelPositionProperty = DependencyProperty.Register("LabelPosition", 
                                                                                                  typeof(IconTextButtonLabelPosition), 
                                                                                                  typeof(IconTextButton), 
                                                                                                  new PropertyMetadata(IconTextButtonLabelPosition.Left));
    public Thickness IconTextMargin
    {
        get => (Thickness)GetValue(IconTextMarginProperty);
        set => SetValue(IconTextMarginProperty, value);
    }
    public static readonly DependencyProperty IconTextMarginProperty = DependencyProperty.Register("IconTextMargin",
                                                                                                   typeof(Thickness),
                                                                                                   typeof(IconTextButton),
                                                                                                   new PropertyMetadata(null));
    public Brush IconBackground
    {
        get { return (Brush)GetValue(IconBackgroundProperty); }
        set { SetValue(IconBackgroundProperty, value); }
    }
    public static readonly DependencyProperty IconBackgroundProperty = DependencyProperty.Register("IconBackground",
                                                                                          typeof(Brush),
                                                                                          typeof(IconTextButton),
                                                                                          new PropertyMetadata(null));

    public double IconRadius
    {
        get => (double)GetValue(IconRadiusProperty);
        set
        {
            SetValue(IconRadiusProperty, value);
            IconBackgroundBorder.Width = value * 2;
            IconBackgroundBorder.Height = value * 2;
            IconBackgroundBorder.CornerRadius = new CornerRadius(value);
        }
    }
    public static readonly DependencyProperty IconRadiusProperty = DependencyProperty.Register("IconRadius",
                                                                                          typeof(double),
                                                                                          typeof(IconTextButton),
                                                                                          new PropertyMetadata(15));
}

我想根據其視覺狀態更改其屬性,但不更改。 這是為什么?

    <VisualState.Setters>
        <Setter Target="LocalShuffleItem.Label" Value="" />
        <Setter Target="LocalListViewItem.Label" Value="" />
        <Setter Target="LocalGridViewItem.Label" Value="" />
        <Setter Target="LocalShuffleItem.IconTextMargin" Value="0" />
        <Setter Target="LocalListViewItem.IconTextMargin" Value="0" />
        <Setter Target="LocalGridViewItem.IconTextMargin" Value="0" />
    </VisualState.Setters>

問題是Label和IconTextMargin不是代碼中的依賴項屬性。

問題是默認的x:Bind模型是OneTime 當您在VisualState編輯屬性的值時,該屬性的值將不會更改。 請檢查IconTextButton類並將x:Bind模式更改為OneWay

<TextBlock
    x:Name="LabelTextBlock"
    Grid.Column="2"
    Margin="{x:Bind IconTextMargin,Mode=OneWay}"
    VerticalAlignment="Center"
    AutomationProperties.AccessibilityView="Raw"
    FontSize="{x:Bind FontSize}"
    FontWeight="{x:Bind FontWeight}"
    Foreground="{x:Bind Foreground}"
    Text="{x:Bind Label, Mode=OneWay}"
    TextAlignment="Center"
    TextWrapping="Wrap" />

暫無
暫無

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

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