how to use dependency property to replace the parameter in the constructor of a UserControl?(如何使用依赖属性替换UserControl的构造函数中的参数?)
问题描述
我注意到以前有人问过类似的问题,但我没有找到任何详细的例子.
I noticed that similar questions have been asked before but I did not find any detailed examples.
我有一个winform程序,它的构造函数有一个参数cn:
I have a winform program, its constructor has a parameter cn:
public AddFailure(ProSimConnect cn)// constructor in winform
{
this.connection = cn;
InitializeComponent();
...
}
现在我需要在 WPF 中的 UserControl 中模拟它并放入 MainWindow.
Now I need to simulate it in a UserControl in WPF and put it into MainWindow.
在 MainWindow.xaml 中:
In MainWindow.xaml:
<Border ...>
<IOS:Core_System/>
</Border>
我想这样做,但我知道我不能这样做,因为它不应该有任何参数:
I want to do this but I know I can't because it shouldn't have any parameter:
public Core_System(ProSimConnect cn)// constructor in UserControl
{
this.cn = connection;
InitializeComponent();
...
}
因此我尝试使用依赖属性:
Therefore I try to use dependency property:
public partial class Core_System : UserControl
{
ProSimConnect connection;
//dependency property
public ProSimConnect cn
{
get { return (ProSimConnect) GetValue(connectionProperty); }
set { SetValue(connectionProperty, value); }
}
public static readonly DependencyProperty connectionProperty =
DependencyProperty.Register("cn", typeof(ProSimConnect),typeof(Core_System));
// constructor in UserControl
public Core_System()
{
this.connection = cn;
InitializeComponent();
...
}
...
}
它不起作用 - 它报告null"异常.我哪里错了?谢谢.
It doesn't work - it reports "null" exception. Where am I wrong? Thanks.
这是需要在UserControl的构造函数中使用参数的函数:
This is the function that need to use the parameter in the constructor of the UserControl:
Failure []getSelectedFailures()
{
return cn.getFailures().Where(failure => failure_name.Contains(failure.name)).ToArray();
}
我称它的位置是:
public partial class Core_System : UserControl
{
...
private void button_Engine_1_On_Fire(object sender, RoutedEventArgs e)
{
...
ArmedFailure.create(getSelectedFailures());
}
}
推荐答案
在构造函数返回之前不能设置依赖属性.
A dependency property cannot be set before the constructor has returned.
您可以将任何使用 ProSimConnect
的代码从 UserControl
的构造函数移动到依赖属性回调:
You could move any code that uses ProSimConnect
from the constructor of the UserControl
to a dependency property callback:
public partial class Core_System : UserControl
{
ProSimConnect connection;
//dependency property
public ProSimConnect cn
{
get { return (ProSimConnect)GetValue(connectionProperty); }
set { SetValue(connectionProperty, value); }
}
public static readonly DependencyProperty connectionProperty =
DependencyProperty.Register("cn", typeof(ProSimConnect), typeof(Core_System), new PropertyMetadata(new PropertyChangedCallback(OnPropertySet));
private static void OnPropertySet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Core_System ctrl = d as Core_System;
ctrl.connection = e.NewValue as ProSimConnect;
//...
}
// constructor in UserControl
public Core_System()
{
InitializeComponent();
}
}
只要将依赖属性设置为一个值,就会调用回调.
The callback will be invoked whenever the dependency property is set to a value.
这篇关于如何使用依赖属性替换UserControl的构造函数中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用依赖属性替换UserControl的构造函数中的参数?


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01