【问题标题】:Custom Dependency Property not showing in Intellisense自定义依赖属性未显示在 Intellisense 中
【发布时间】:2017-12-01 14:41:18
【问题描述】:

我在后面的代码中创建了以下自定义Dependency Property。 这个 infragistics XamDataGrid 类型的依赖属性等等的所有者。 我正在尝试通过此属性获取网格的引用。

以下代码编译时没有错误或警告。但是,Dependency Property 不会显示在 XAML intelliSense 中。

我也尝试过输入全名。它不承认这个DP。 我已经清理了项目并重建了它。 我什至关闭了 Visual Studio 并重新打开它。

using System.Windows;
using Infragistics.Windows.DataPresenter;

namespace Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty DPRProperty = 
            DependencyProperty.Register(
                            "DPR", 
                            typeof(XamDataGrid), 
                            typeof(MainWindow), 
                            new FrameworkPropertyMetadata(null));

        public XamDataGrid DPR
        {
            get { return (XamDataGrid)GetValue(DPRProperty); }
            set { SetValue(DPRProperty, value); }
        }

    }
}

【问题讨论】:

  • 你试过了吗:清洁解决方案 + 重建
  • @jHilscher 是的。我在问题中指定了这一点。
  • 您的意思是当您在&lt;Window ...&gt; 标记内输入XAML 编辑器时?
  • @Clemens 在 Window 标签中是。
  • 嗯,那里是Window,而不是MainWindow。但无论如何,这一切似乎都没有意义。您将如何分配 XamDataGrid?您可能想要做的就是在 MainWindow 的 XAML 中的某处添加一个 XamDataGrid,并分配 x:Name 属性,这将为您生成一个访问该元素的成员变量。

标签: c# wpf xaml dependency-properties infragistics


【解决方案1】:

问题是您希望依赖属性显示在您的基类 XAML“代码”中

但是,您没有为 Window 创建 DP,而是为 MainWindow 创建 DP

如果您使用&lt;MainWindow /&gt; 标签,它会显示出来

如果您能解释一下您尝试归档的内容,可能会进一步帮助您

编辑

我想我现在明白你的目标是什么

要获得对任何对象的引用,您不需要 DP,而是必须正确排序所有内容

需要的东西:

  • 窗户(显然)
  • 数据上下文
  • 一些代码隐藏
  • 附加属性

附加属性看起来很像这样

public class Initialized
{
    public static DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command",
        typeof(ICommand),
        typeof(Initialized),
        new UIPropertyMetadata(CommandChanged));

    public static DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof(object),
                                            typeof(Initialized),
                                            new UIPropertyMetadata(null));

    public static void SetCommand(DependencyObject target, ICommand value)
    {
        target.SetValue(CommandProperty, value);
    }

    public static void SetCommandParameter(DependencyObject target, object value)
    {
        target.SetValue(CommandParameterProperty, value);
    }
    public static object GetCommandParameter(DependencyObject target)
    {
        return target.GetValue(CommandParameterProperty);
    }

    private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        var type = target.GetType();
        var ev = type.GetEvent("Initialized");
        var method = typeof(Initialized).GetMethod("OnInitialized");

        if ((e.NewValue != null) && (e.OldValue == null))
        {
            ev.AddEventHandler(target, Delegate.CreateDelegate(ev.EventHandlerType, method));
        }
        else if ((e.NewValue == null) && (e.OldValue != null))
        {
            ev.RemoveEventHandler(target, Delegate.CreateDelegate(ev.EventHandlerType, method));
        }
    }

    public static void OnInitialized(object sender, EventArgs e)
    {
        var control = sender as FrameworkElement;
        var command = (ICommand)control.GetValue(CommandProperty);
        var commandParameter = control.GetValue(CommandParameterProperty);
        command.Execute(commandParameter);
    }
}

在您的 DataContext(我们将其命名为 MainWindowDataContext)中,创建一个新的 ICommand 属性(我们将其命名为 CmdInitialized),将传递给命令的参数放入您的实例变量中

然后,在您的 XAML 代码中,您只需像往常一样使用 AttachedProperty

 <!-- `ev:` is the XAML namespace the Initialized class is located in -->
 ev:Initialized.Command="{Binding CmdInitialized}"
 ev:Initialized.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"

然而,重要的部分是:DataContext 需要在组件初始化之前已经附加到窗口中

这意味着您必须将窗口代码编辑为以下内容:

public MainWindow()
{
    this.DataContext = new MainWindowDataContext();
    InitializeComponent();
}

以后应该没事了

如果您需要 ICommand 的解决方案,请在 google 上搜索 RelayCommand :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2011-11-13
    • 2016-01-07
    • 1970-01-01
    • 2012-05-30
    • 2013-10-06
    相关资源
    最近更新 更多