我需要为现有的 WPF 控件(组框、文本框、复选框等)创建一个新属性,该属性将存储其访问级别,因此我找到了附加属性。我以这个网站为例http://dotnetbyexample.blogspot.com.br/2010/05/attached-dependency-properties-for.html
一切都很好,但是尝试在某些控件上使用它时出现以下错误...
错误 1 XML 命名空间“clr-namespace:ImageGUI.App_Code;assembly=ImageGUI”中不存在属性“DependencyPropertiesHoster.AcessLevel”。第 131 行位置 97.ImageGUI\MainWindow.xaml 131 97 ImageGUI
这是我的 C# 代码片段...
namespace ImageGUI.App_Code
{
public static class DependencyPropertiesHoster
{
//[AttachedPropertyBrowsableForChildren]
public static readonly DependencyProperty AcessLevelProperty =
DependencyProperty.RegisterAttached(
"AcessLevel",
typeof(EAcessLevel),
typeof(DependencyPropertiesHoster),
new PropertyMetadata(AcessLevelChanged)
);
// Called when Property is retrieved
public static EAcessLevel GetAcessLevel(DependencyObject obj)
{
if (obj != null)
return (EAcessLevel)obj.GetValue(AcessLevelProperty);
else
return EAcessLevel.Client;
//return obj.GetValue(AcessLevelProperty) as EAcessLevel;
}
// Called when Property is set
public static void SetAcessLevel(DependencyObject obj, EAcessLevel value)
{
obj.SetValue(AcessLevelProperty, value);
}
// Called when property is changed
private static void AcessLevelChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var attachedObject = sender as UIElement;
if (attachedObject != null)
{
// do whatever is necessary, for example
// attachedObject.CallSomeMethod(
// args.NewValue as TargetPropertyType);
}
}
}
}
这是我在窗口的声明
xmlns:CustomDepen="clr-namespace:ImageGUI.App_Code;assembly=ImageGUI"
这是我对该属性的使用(错误所在......)
<GroupBox Name="gbApplications" Header="{DynamicResource applications}" CustomDepen:DependencyPropertiesHoster.AcessLevel="Client">
观察:EAcessLevel 只是一个简单的枚举器。
提前致谢。