通过此设置依赖项 属性
Setting A dependency property by this
我创建了一种名为 SelectableBorder 的边框类型,它还有一个名为 "IsSelected" 的附加 属性。我在某些触发器中使用了这个 属性 例如:
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="{DynamicResource AccentColorBrush3}" />
</MultiTrigger.Setters>
但是在后台的代码中我无法设置 IsSelected,我该如何创建一个可以在 xaml 触发器和代码中使用的 属性背景?
这是当前的 SelectableBorder 代码
public class SelectableBorder : Border
{
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(Border), new PropertyMetadata(false));
public static void SetIsSelected(UIElement element, bool value)
{
element.SetValue(IsSelectedProperty, value);
}
public static bool GetIsSelected(UIElement element)
{
return (bool)element.GetValue(IsSelectedProperty);
}
}
我也看到了:
'SelectableBorder' 初始化失败:'SelectableBorder' 的类型初始化器抛出异常。
这表明我在第一次尝试时表现不佳。能否请您指导我解决这些问题的正确方向?
由于您要将 属性 添加到 DependencyObject
,因此您可以使用普通的 DependencyProperty
而不是附加的 DependencyProperty
。如果你愿意,你可以使用附加的,但触发器应该改变。此外,您的 属性 所有者类型应为 SelectableBorder
而不是 Border
。您还可以添加 IsSelected
CLR 包装器,这样可以更轻松地 set/get 代码隐藏中的值。
public class SelectableBorder : Border
{
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(SelectableBorder), new PropertyMetadata(false));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
}
我创建了一种名为 SelectableBorder 的边框类型,它还有一个名为 "IsSelected" 的附加 属性。我在某些触发器中使用了这个 属性 例如:
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="{DynamicResource AccentColorBrush3}" />
</MultiTrigger.Setters>
但是在后台的代码中我无法设置 IsSelected,我该如何创建一个可以在 xaml 触发器和代码中使用的 属性背景?
这是当前的 SelectableBorder 代码
public class SelectableBorder : Border
{
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(Border), new PropertyMetadata(false));
public static void SetIsSelected(UIElement element, bool value)
{
element.SetValue(IsSelectedProperty, value);
}
public static bool GetIsSelected(UIElement element)
{
return (bool)element.GetValue(IsSelectedProperty);
}
}
我也看到了:
'SelectableBorder' 初始化失败:'SelectableBorder' 的类型初始化器抛出异常。
这表明我在第一次尝试时表现不佳。能否请您指导我解决这些问题的正确方向?
由于您要将 属性 添加到 DependencyObject
,因此您可以使用普通的 DependencyProperty
而不是附加的 DependencyProperty
。如果你愿意,你可以使用附加的,但触发器应该改变。此外,您的 属性 所有者类型应为 SelectableBorder
而不是 Border
。您还可以添加 IsSelected
CLR 包装器,这样可以更轻松地 set/get 代码隐藏中的值。
public class SelectableBorder : Border
{
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(SelectableBorder), new PropertyMetadata(false));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
}