我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“代码”中
但是,您没有为Window
但是为MainWindow
如果你会使用一个<MainWindow />
标签,它会出现
如果您可以解释您尝试存档的内容,则可能会进一步帮助您
编辑
我想我现在明白你的目标是什么了
要获得对任何对象的引用,您不需要 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 的解决方案,请RelayCommand
在 google 上搜索 :)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句