自定义依赖属性未在Intellisense中显示

时间:2017-12-01 14:41:19

标签: c# wpf xaml dependency-properties infragistics

我在后面的代码中创建了以下自定义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); }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

问题是你希望Dependency Property出现在你的基类XAML&#34; code&#34;

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

创建了DP

如果您使用MainWindow标记,则会显示

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

<强> 修改

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

要获得对任何对象的引用,您不需要DP,而是必须正确地订购所有对象

需要的东西:

  • 窗口(显然)
  • DataContext
  • 一些代码背后
  • 附属物

附属物看起来非常像这样

<MainWindow />

在DataContext中(让它命名为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); } } ),创建一个新的MainWindowDataContext属性(让它命名为ICommand),将传递给命令的参数放入实例变量

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

CmdInitialized

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

这意味着你必须编辑你的窗口代码:

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

之后,你应该没事了

如果您需要ICommand的解决方案,请在Google上搜索public MainWindow() { this.DataContext = new MainWindowDataContext(); InitializeComponent(); } :)

相关问题