【问题标题】:Dependency Property in WPFWPF 中的依赖属性
【发布时间】:2012-11-27 22:37:37
【问题描述】:

我正在为我的 Avalon 坞站控制器开发 DependencyProperty。这是我目前正在处理的一些示例代码。

要求是:在一个类中创建所有依赖属性,并在 View 中访问该属性。像这样的。

<Button isPaneVisible="true"> or <Button isPaneVisible="{Staticresource path=name, Mode=twoway">

您能帮我解决这个问题吗?

namespace Avatar.UI.ViewModel
{
    internal class DependencyPropertyClass : DependencyObject
    {
        public static readonly DependencyProperty IsPaneVisibleProperty =
            DependencyProperty.RegisterAttached("IsPaneVisible", typeof(bool), typeof(DependencyPropertyClass),
                new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsPaneVisible_PropertyChanged));

        private static void IsPaneVisible_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        /// <summary>
        /// Sets the IsPaneVisible for an element.
        /// </summary>
        public bool IsPaneVisible
        {
            get { return (bool)GetValue(IsPaneVisibleProperty); }
            set
            {
                SetValue(IsPaneVisibleProperty, value);
            }
        }

    }
}

<UserControl x:Class="Avatar.UI.View.ContentView"             
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
             mc:Ignorable="d" 
             xmlns:avalonDock="http://avalondock.codeplex.com"     
             xmlns:local="clr-namespace:Avatar.UI.ViewModel"             
             d:DesignHeight="300" d:DesignWidth="300">


<Button IsPaneVisible="true"></Button 

</UserControl>

【问题讨论】:

  • 您要求我们帮助您解决问题,但您的问题中没有描述问题。请准确说明您的期望和得到的结果
  • 似乎您正在尝试使用 DependencyProperty 作为附加属性,请参阅stackoverflow.com/questions/7148946/attached-properties 以了解说明
  • 疯狂猜测:不要将类设为内部可能会导致 XAML 解析器在内部类上使用反射。
  • 记得接受答案或发布您自己的答案。这将帮助您获得未来问题的答案(因为您的“接受”评级与您的用户 ID 一起发布)

标签: c# wpf dependencies


【解决方案1】:

我可能是错的,但我认为您正在寻找这个:

<Button local:DependencyPropertyClass.IsPaneVisible="true"></Button>

您必须指定命名空间,因为 IsPaneVisible 不属于“http://schemas.microsoft.com/winfx/2006/xaml/presentation”命名空间。

见:Attached Properties Overview

编辑
自从我这样做以来已经有一段时间了,所以当我扫描你的代码时,事情正在慢慢地回到我身边。对于附加属性,您不能使用实例属性来获取/设置属性。您必须创建静态 Get&lt;PropertyName&gt;Set&lt;PropertyName&gt; 函数:

public static void SetIsPaneVisible(DependenyObject target, Boolean value)
{
    target.SetValue(IsPaneVisibleProperty, value);
}
public static bool GetIsPaneVisible(DependenyObject target)
{
    return (bool)target.GetValue(IsPaneVisibleProperty);
}

说真的...请阅读链接的文章。那里都有解释。

【讨论】:

  • xmlns:local="clr-namespace:Avatar.UI.ViewModel" 这是我正在使用的命名空间。
  • 更新了我的答案。我认为你也需要使用类名。
  • 另外,它表示该属性不属于依赖对象。
  • 进行上述更改后,我收到此错误:本地属性“IsPaneVisible”只能应用于从“DependencyPropertyClass”派生的类型。
  • 这里显示的设置器是错误的。它应该调用target.SetValue(IsPaneVisibleProperty, value) 而不是element.SetValue(IsIsPaneVisible, target)。吸气剂也应阅读target.GetValue(IsPaneVisibleProperty)
【解决方案2】:

定义附加的依赖属性还需要定义静态 get 和 set 访问器方法。请参阅Custom Attached Properties 了解更多信息。另请注意,您的类不一定需要从 DependencyObject 派生,只要它只定义附加属性即可。但是在 public 类中定义这些属性总是一个好主意。

public class DependencyPropertyClass
{
    public static readonly DependencyProperty IsPaneVisibleProperty =
        DependencyProperty.RegisterAttached("IsPaneVisible", typeof(bool), typeof(DependencyPropertyClass),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsPaneVisible_PropertyChanged));

    private static void IsPaneVisible_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }

    public static bool GetIsPaneVisible(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsPaneVisibleProperty);
    }

    public static void SetIsPaneVisible(DependencyObject obj, bool value)
    {
        obj.SetValue(IsPaneVisibleProperty, value);
    }
}

正如 Cyborgx37 所指出的,您可以像这样在 XAML 中使用附加属性:

<Button local:DependencyPropertyClass.IsPaneVisible="True" />

【讨论】:

  • 进行上述更改后,我收到此错误:本地属性“IsPaneVisible”只能应用于从“DependencyPropertyClass”派生的类型。
  • 你是按照这里显示的还是按照 Cyborgx37 显示的那样做?他的二传手错了。
  • 是的,我试过了,但没有运气。但是 IsPanelVisible 能够在主窗口中访问,而不是在用户控制中。
  • 你还没有回答我的问题。再说一次,你是按照这里显示的还是按照 Cyborgx37 显示的那样做?
  • 大家有没有试过访问用户控件中定义的依赖属性并在用户控件中访问它自己?。
【解决方案3】:

依赖属性应该派生您要为其创建依赖属性的基本类。例如,如果您要为按钮创建依赖属性,则将基类按钮派生到您的类。

这就是我解决问题的方法。

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    相关资源
    最近更新 更多