[英]WPF - Passing parameter from Dialog Window to UserControl
I'm using DependencyProperty to pass parameters from DialogWindow to UserControl. 我正在使用DependencyProperty将参数从DialogWindow传递到UserControl。 I want that parameter inside the UserControl constructor in order to pass it to the view model.
我想要在UserControl构造函数中将该参数传递给视图模型。
The property always returns null, and never get set. 该属性始终返回null,并且永远不会设置。
Code: 码:
MainWindow: 主窗口:
var dialog = new DialogWindow();
dialog.ShowDialog();
DialogWindow.xaml DialogWindow.xaml
<TabControl>
<TabItem Header="Data Source">
<local:DataSourceView Test="Something" />
</TabItem>
</TabControl>
DataSourceView.xaml.cs DataSourceView.xaml.cs
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register("Test", typeof(object), typeof(DataSourceView));
public object Test {
get { return (object)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public DataSourceView() {
InitializeComponent();
DataContext = new DataSourceViewModel(Test);// Test is always null
}
You can use overload of DependencyProperty.Register
method with PropertyMetadata
which will have a PropertyChangedCallback
parameter and observe data binding process. 您可以将
DependencyProperty.Register
方法的重载与PropertyMetadata
,该方法将具有PropertyChangedCallback
参数并观察数据绑定过程。
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register("Test", typeof(object), typeof(DataSourceView), new PropertyMetadata(TestPropertyChangedCallback
));
private static void TestPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.NewValue);
System.Diagnostics.Debug.WriteLine(e.OldValue);
}
Following link contains the solution for a similar problem: 以下链接包含类似问题的解决方案:
Passing Parameters between xaml window and usercontrol WPF 在xaml窗口和usercontrol WPF之间传递参数
"The property always returns null, and never get set." “该属性始终返回null,并且永远不会被设置。” - Is wrong.
- 是错的。 The property is null in DataSourceView constructor.
在DataSourceView构造函数中,该属性为null。 After the instance of DataSourceView is created, property will be set to "Something".
创建DataSourceView实例后,属性将设置为“ Something”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.