Wpf样式和附加属性
我一直在玩弄行为,我遇到了一个有趣的问题。 以下是我的行为:Wpf样式和附加属性,wpf,styles,dependency-properties,attached-properties,Wpf,Styles,Dependency Properties,Attached Properties,我一直在玩弄行为,我遇到了一个有趣的问题。 以下是我的行为: public class AddNewBehavior : BaseBehavior<RadGridView, AddNewBehavior> { public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(Add
public class AddNewBehavior : BaseBehavior<RadGridView, AddNewBehavior>
{
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(AddNewBehavior), new FrameworkPropertyMetadata(false, OnIsEnabledChanged));
public static void SetIsEnabled(DependencyObject obj, bool isEnabled)
{
obj.SetValue(IsEnabledProperty, isEnabled);
}
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
} ... OnIsEnabledChanged(...)}
public类AddNewBehavior:BaseBehavior
{
public static readonly dependencProperty IsEnabledProperty=dependencProperty.RegisterAttached(“IsEnabled”、typeof(bool)、typeof(AddNewBehavior)、new FrameworkPropertyMetadata(false、OniseEnabledChanged));
公共静态无效设置已启用(DependencyObject对象,布尔已启用)
{
对象设置值(IsEnabledProperty,isEnabled);
}
公共静态bool GetIsEnabled(DependencyObject obj)
{
返回(bool)对象GetValue(IsEnabledProperty);
}…OnIsEnabledChanged(…)}
当我设置如下样式时,这将非常有效:
<Style TargetType="telerikGridView:RadGridView">
<Setter Property="Behaviors:AddNewBehavior.IsEnabled" Value="true" />
</Style>
但是如果我把它放在一个抽象类中
public abstract class BaseBehavior<TObj, TBehavior> : Behavior<TObj>
where TObj : DependencyObject
where TBehavior : BaseBehavior<TObj, TBehavior>, new()
{
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(TBehavior), new FrameworkPropertyMetadata(false, OnIsEnabledChanged));
public static void SetIsEnabled(DependencyObject obj, bool isEnabled)
{
obj.SetValue(IsEnabledProperty, isEnabled);
}
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
public static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
{
BehaviorCollection behaviorCollection = Interaction.GetBehaviors(dpo);
if ((bool)e.NewValue)
{
var firstOrDefault = behaviorCollection.Where(b => b.GetType() == typeof(TBehavior)).FirstOrDefault();
if (firstOrDefault == null)
{
behaviorCollection.Add(new TBehavior());
}
}
}
}
公共抽象类BaseBehavior:行为
其中TObj:DependencyObject
其中TBehavior:BaseBehavior,new()
{
public static readonly dependencProperty IsEnabledProperty=dependencProperty.RegisterAttached(“IsEnabled”,typeof(bool),typeof(TBehavior),new FrameworkPropertyMetadata(false,oniseEnabledChanged));
公共静态无效设置已启用(DependencyObject对象,布尔已启用)
{
对象设置值(IsEnabledProperty,isEnabled);
}
公共静态bool GetIsEnabled(DependencyObject obj)
{
返回(bool)对象GetValue(IsEnabledProperty);
}
公共静态无效OnInsEnabledChanged(DependencyObject dpo、DependencyPropertyChangedEventArgs e)
{
BehaviorCollection BehaviorCollection=Interaction.GetBehaviors(dpo);
if((bool)e.NewValue)
{
var firstOrDefault=behaviorCollection.Where(b=>b.GetType()==typeof(TBehavior)).firstOrDefault();
if(firstOrDefault==null)
{
添加(新的TBehavior());
}
}
}
}
样式声明将使用“值不能为null。属性名称:属性”压碎
不知道我做错了什么,在基类中启用代码将是一件好事
谢谢,在基类中的IsEnabledProperty定义中,尝试将其更改为:
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached(
"IsEnabled",
typeof(bool),
typeof(BaseBehavior<TObj, TBehavior>),
new FrameworkPropertyMetadata(false, OnIsEnabledChanged)
);
公共静态只读从属属性IsEnabledProperty=
DependencyProperty.RegisterAttached(
“我被逮捕”,
类型(bool),
类型(基本行为),
新的FrameworkPropertyMetadata(false,onSenabledChanged)
);
也就是说,不要将
TBehavior
作为DP所有者类型传递,而是传递BaseBehavior
。太好了,这就成功了,10倍。昨晚我试这个的时候一定睡得很熟。