wpf 如何动态加载用户控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1774962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I load user controls dynamically?
提问by Muhammad Adnan
How can I load a user control[s] in a window dynamically (using code at runtime)?
如何在窗口中动态加载用户控件[s](在运行时使用代码)?
回答by Richard Szalay
I'd highly recommend having a look at Prism, since composite user interfaces is what it's for. However, since this would require you refactoring your entire application, I'll also answer your question directly.
我强烈建议看看Prism,因为复合用户界面就是它的用途。但是,由于这需要您重构整个应用程序,因此我也将直接回答您的问题。
If you want a single user control in a container, put a ContentControl in your XAML and then set the Content property. If you are using a view model, you could bind Content to a FrameworkElement property on the view model:
如果需要在容器中使用单个用户控件,请将 ContentControl 放入 XAML,然后设置 Content 属性。如果您使用的是视图模型,则可以将 Content 绑定到视图模型上的 FrameworkElement 属性:
contentControlInstance.Content = new CustomUserControl();
If you want multiple controls in a list, use an ItemsControl and assign an ObservableCollection<> to the ItemsSource property. If you are using a view model, you could bind ItemsSource to an ObservableCollection property on the View Model.
如果您希望在一个列表中有多个控件,请使用 ItemsControl 并将 ObservableCollection<> 分配给 ItemsSource 属性。如果您使用的是视图模型,则可以将 ItemsSource 绑定到视图模型上的 ObservableCollection 属性。
Then you can just add/remove views from that ObservableCollection:
然后你可以从 ObservableCollection 添加/删除视图:
private ObservableCollection<FrameworkElement> views =
new ObservableCollection<FrameworkElement>();
private void Initialize()
{
itemsControl.ItemsSource = views;
}
private void AddView(FrameworkElement frameworkElement)
{
views.Add(frameworkElement);
}
回答by Sasikumar D.R.
For adding multiple controls you need container.
要添加多个控件,您需要容器。
Suppose you have a StackPanel container "myStack"
假设您有一个 StackPanel 容器“myStack”
<Window ..>
<StackPanel Name="MyStack" />
</Window>
You can create control dynamically and add it to container. See code below
您可以动态创建控件并将其添加到容器中。见下面的代码
void AddButtons()
{
Button B1=new Button(),B2=new Button(), B3=new Button();
B1.Content="Hello";
B2.Content="First";
B3.content="Application";
// Now you can set more properties like height, width, margin etc...
MyStack.Children.Add(B1);
MyStack.Children.Add(B2);
MyStack.Children.Add(B2);
}
回答by hashlock
Or use binding. Here's a really crude example showing how different WPF controls can be shown in a single WPF window using ContentControl and binding (which is what a toolkit like Prism or Caliburn Micro does).
或者使用绑定。这是一个非常粗略的示例,展示了如何使用 ContentControl 和绑定(这是像 Prism 或 Caliburn Micro 这样的工具包所做的)在单个 WPF 窗口中显示不同的 WPF 控件。
XAML:
XAML:
<UserControl x:Class="ViewA">
...
<UserControl/>
<UserControl x:Class="ViewB">
...
<UserControl/>
Code:
代码:
void ShowViewModelDialog (object viewModel)
{
var host = new MyViewHost();
FrameworkElement control = null;
string viewModelName = viewModel.GetType().Name;
switch (viewModelName )
{
case ("ViewModelA"):
control = new ViewA();
break;
case ("ViewModelB"):
control = new ViewB();
break;
default:
control = new TextBlock {Text = String.Format ("No view for {0}", viewModelName);
break;
}
if (control!=null) control.DataContext = viewModel;
host.DataContext = control;
host.Show(); // Host window will show either ViewA, ViewB, or TextBlock.
}