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>
The problem is Label and IconTextMargin is not dependency property in your code.
问题是默认的 x:Bind
模型是 OneTime
。当您在 VisualState
中编辑它们的值时,属性 的值不会更改。请检查 IconTextButton
class 并将 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" />
我有一个名为 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>
The problem is Label and IconTextMargin is not dependency property in your code.
问题是默认的 x:Bind
模型是 OneTime
。当您在 VisualState
中编辑它们的值时,属性 的值不会更改。请检查 IconTextButton
class 并将 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" />