WPF Style 中 设立 自定义的附加属性 出错
WPF Style 中 设置 自定义的附加属性 出错
我写了一个附加属性
如下使用 会出错
但是 Grid.Row 都是附加属性,但在Style 中设置没有任何问题。我写的附加属性就有问题。。。
这是为什么。谢谢大家
------解决思路----------------------
整个貌似是 依赖属性,不是附加属性。
------解决思路----------------------
因为你定义的附加属性名称为“IsPressed”,所以GetIsPressedProperty改为GetIsPressed,SetIsPressedProperty改为SetIsPressed,绑定是local:ExtAttachProperty.IsPressed,而不是local:ExtAttachProperty.IsPressedProperty
------解决思路----------------------
2楼完全正解!
依赖项属性用错了、
我写了一个附加属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WpfApplication1
{
public class ExtAttachProperty : DependencyObject
{
public static bool GetIsPressedProperty(DependencyObject obj)
{
return (bool)obj.GetValue(IsPressedProperty);
}
public static void SetIsPressedProperty(DependencyObject obj, bool value)
{
obj.SetValue(IsPressedProperty, value);
}
public static readonly DependencyProperty IsPressedProperty =
DependencyProperty.RegisterAttached("IsPressed", typeof(bool), typeof(ExtAttachProperty), new PropertyMetadata(false));
}
}
如下使用 会出错
<Canvas Grid.Row="1" >
<Canvas.Resources>
<Style TargetType="{x:Type Canvas}">
<Setter Property="local:ExtAttachProperty.IsPressedProperty" Value="True"></Setter>
<Setter Property="Background" Value="#d0d1d7"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#888888"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
</Style.Triggers>
</Style>
</Canvas.Resources>
</Canvas>
但是 Grid.Row 都是附加属性,但在Style 中设置没有任何问题。我写的附加属性就有问题。。。
这是为什么。谢谢大家
------解决思路----------------------
整个貌似是 依赖属性,不是附加属性。
------解决思路----------------------
因为你定义的附加属性名称为“IsPressed”,所以GetIsPressedProperty改为GetIsPressed,SetIsPressedProperty改为SetIsPressed,绑定是local:ExtAttachProperty.IsPressed,而不是local:ExtAttachProperty.IsPressedProperty
------解决思路----------------------
2楼完全正解!
依赖项属性用错了、