自定义依赖项 属性 未在 Intellisense 中显示
Custom Dependency Property not showing in Intellisense
我在后面的代码中创建了以下自定义 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); }
}
}
}
问题是您希望依赖关系 属性 出现在您的基础中 类 XAML "code"
但是,您没有为 Window
创建 DP,而是为 MainWindow
如果您使用 <MainWindow />
标签,它会显示
如果您能解释一下您尝试存档的内容,有人可能会进一步帮助您
编辑
我想我现在明白你的目标是什么了
要获得对任何对象的引用,您不需要 DP,而是必须正确排序所有内容
需要的东西:
- Window(显然)
- 一个数据上下文
- 一些代码隐藏
- 附上-属性
附件 属性 看起来很像这样
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 代码中,您只需像往常一样使用 Attached属性
<!-- `ev:` is the XAML namespace the Initialized class is located in -->
ev:Initialized.Command="{Binding CmdInitialized}"
ev:Initialized.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
然而重要的部分是:DataContext 需要在组件初始化之前已经附加到 window
这意味着您必须将 window 代码编辑为如下内容:
public MainWindow()
{
this.DataContext = new MainWindowDataContext();
InitializeComponent();
}
之后,你应该没事
如果您需要 ICommand 的解决方案,请在 google 上搜索 RelayCommand
:)
我在后面的代码中创建了以下自定义 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); }
}
}
}
问题是您希望依赖关系 属性 出现在您的基础中 类 XAML "code"
但是,您没有为 Window
创建 DP,而是为 MainWindow
如果您使用 <MainWindow />
标签,它会显示
如果您能解释一下您尝试存档的内容,有人可能会进一步帮助您
编辑
我想我现在明白你的目标是什么了
要获得对任何对象的引用,您不需要 DP,而是必须正确排序所有内容
需要的东西:
- Window(显然)
- 一个数据上下文
- 一些代码隐藏
- 附上-属性
附件 属性 看起来很像这样
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 代码中,您只需像往常一样使用 Attached属性
<!-- `ev:` is the XAML namespace the Initialized class is located in -->
ev:Initialized.Command="{Binding CmdInitialized}"
ev:Initialized.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
然而重要的部分是:DataContext 需要在组件初始化之前已经附加到 window
这意味着您必须将 window 代码编辑为如下内容:
public MainWindow()
{
this.DataContext = new MainWindowDataContext();
InitializeComponent();
}
之后,你应该没事
如果您需要 ICommand 的解决方案,请在 google 上搜索 RelayCommand
:)