当前用户控件名称 [英] Current User Control Name
本文介绍了当前用户控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI,
我有MainWindow和10个用户控件。我在MainWindow上显示usercontrols。我怎么知道Mainwindow中存在哪个用户控件。
谢谢。
I have MainWindow and 10 User Controls. I am showing usercontrols on MainWindow. How would i know which usercontrol is present in Mainwindow.
Thanks.
推荐答案
如果您关注mvvm设计模式有这样的步骤:
1.在你的userControl模型中创建一个属性并将其命名为ControlId。
2.将userControls添加到某个变量:例如List或ConcurrentBag或其他什么。
3.将此绑定到您的视图(MainWIndow)。
4.在MainWIndowViewModel上实现事件以检查userControls。
示例:
YourUserControlModel.cs:
If you are following mvvm design pattern there are such steps:
1. In your userControl model create a property and call it ControlId.
2. Add userControls to some variable: for example List or ConcurrentBag or whatever.
3. Bind this to your view(MainWIndow).
4. Implement Event on your MainWIndowViewModel to check for userControls.
Example:
YourUserControlModel.cs :
public int ControlId{get;set;}
设置此属性的某处。
在构造函数中加载用户控件,如下所示:
Set somewhere this property.
Load your usercontrols in constructor like this:
public MainWindowViewModel()
{
Controls.Add(new YourUserControl());
}
private readonly DeferrableObservableCollection<YourUserControl> Controls = new DeferrableObservableCollection<YourUserControl>();
查找userControls的事件:
Event to look for userControls:
void SomeName(object Sender, EventArgs e)
{
foreach(var control in Controls)
{
if(control.ViewModel.Model.ControlId == 1) // checks, if collection has userControl with id 1. And so on for all controls
}
}
希望这对你有帮助。
如果您有任何问题,请随时提出。
Hope this helped you.
If you have questions feel free to ask.
这篇关于当前用户控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文