Wpf 样式和附加属性
我一直在研究行为,发现了一个有趣的问题。 这是我的行为:
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(...)}
当我设置这样的样式时,这会很好用:
<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());
}
}
}
}
样式声明将被“值不能为空。属性名称:属性”压垮。
想知道我做错了什么,在基类中使用 IsEnabled 代码会很棒。
谢谢,
I've been playing with behaviors and I came across a interesting issue.
Here is my behavior:
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(...)}
This will work great when I set the styles like this:
<Style TargetType="telerikGridView:RadGridView">
<Setter Property="Behaviors:AddNewBehavior.IsEnabled" Value="true" />
</Style>
But if I put this in an abstract class
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());
}
}
}
}
The style declaration will crush with "Value cannot be null. Property name: property".
Wonder what am I doing wrong, it will be great to have to IsEnabled code in a base class.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在基类的 IsEnabledProperty 定义中,尝试将其更改为:
即,不传递
TBehavior
作为 DP OwnerType,而是传递BaseBehavior
。In your IsEnabledProperty definition in the base class, try changing it to:
that is, instead of passing
TBehavior
as the DP OwnerType, pass inBaseBehavior<TObj, TBehavior>
instead.