簡體   English   中英

C# WPF 附加屬性 - 錯誤:“XML 命名空間中不存在該屬性”

[英]C# WPF Attached Properties - Error: “The property does not exist in XML namespace”

我需要為現有的 WPF 控件(Groupbox、文本框、復選框等)創建一個新屬性,一個將存儲其訪問級別的屬性,因此我找到了附加屬性。 我以這個網站為例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 只是一個簡單的枚舉器。

提前致謝。

感謝 Bob 和 Kent 的回答,這幾乎解決了這個問題。 在這種情況下,只需改變

xmlns:CustomDepen="clr-namespace:ImageGUI.App_Code;assembly=ImageGUI"

xmlns:CustomDepen="clr-namespace:ImageGUI.App_Code"

解決了這個問題。 其他一切都是正確的。

關於我關於如何檢索指定值的其他評論,它會是這樣的:

EAcessLevel currentAcess = (EAcessLevel)gbApplications.GetValue(DependencyPropertiesHoster.AcessLevelProperty);

謝謝,希望它也能在未來對某人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM