在 WPF 中以编程方式设置 ContentControl 的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21310072/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:34:56  来源:igfitidea点击:

Programmatically setting the content of a ContentControl in WPF

c#wpfxaml

提问by SimonAx

I have a UserControl named "WorkspaceView" and its only purpose is to show other views as tabs. Call these views ViewA, ViewB etc. Which of these views to present should be determined on runtime, so I figured I needed a control that can present ... well ..stuff.
ContentControl to the rescue. Except ... I can't make it work. I'm trying to new up a usercontrol of type ViewA in the code behind and assign it to my MyContent, which is the ContentControl. I've tried:

我有一个名为“WorkspaceView”的 UserControl,它的唯一目的是将其他视图显示为选项卡。将这些视图称为 ViewA、ViewB 等。应该在运行时确定要呈现的这些视图中的哪一个,所以我想我需要一个可以呈现……嗯……东西的控件。
内容控制来救援。除了......我不能让它工作。我正在尝试在后面的代码中新建一个 ViewA 类型的用户控件,并将其分配给我的 MyContent,即 ContentControl。我试过了:

public WorkspaceView()
{
    InitializeComponent();
    DataContext = new View(A); //Hoping that the DataContext will propagate down
}

Second attempt was

第二次尝试是

public WorkspaceView()
{
    InitializeComponent();
    var binding = new Binding {Source = new ViewA()};
    MyContent.SetBinding(ContentControl.ContentProperty, binding);
}

In both cases, I see an empty box, but since I've hard wired a TextBlock into ViewA, I'd expect it to show me that text. What am I doing wrong?

在这两种情况下,我都会看到一个空框,但由于我已将 TextBlock 硬连接到 ViewA,我希望它向我显示该文本。我究竟做错了什么?

Despite knowing that MVVM is the preferred way to develop WPF applications, I'd prefer to see how I can do this with code behind files. Later on, I will redo the application with MVVM, but first I need to get some basic understanding of WPF.

尽管知道 MVVM 是开发 WPF 应用程序的首选方式,但我更愿意看看如何使用代码隐藏文件来做到这一点。稍后,我将使用 MVVM 重做应用程序,但首先我需要对 WPF 有一些基本的了解。


In response to the suggestions so far, I've tried


为了回应到目前为止的建议,我已经尝试过

MyContent.Content = new ViewA();

but still I the text that is in ViewA does not appear. I've also at the bottom of this post included a screenshot of what the application renders.

但我仍然没有出现在 ViewA 中的文本。我还在这篇文章的底部包含了应用程序呈现内容的屏幕截图。

WorkspaceView

工作区视图

enter image description here

在此处输入图片说明

Resource file

资源文件

enter image description here

在此处输入图片说明

What is rendered

呈现什么

enter image description here

在此处输入图片说明

回答by jnovo

Have you tried simply doing this?

你试过简单地这样做吗?

MyContent.Content = new ViewA();

EDIT

编辑

Try simplifying your code a bit and working from there. For instance:

尝试稍微简化您的代码并从那里开始工作。例如:

public WorkspaceView()
{
   InitializeComponent();

   // Something better than UserControl should be used here
   ObservableCollection<UserControl> views = new ObservableCollection<UserControl>();
   views.Add(new ViewA());
   views.Add(new ViewB());

   DataContext = views;

}

<Border ..>
    <TabControl x:Name="TabControl"
       ..
       ItemsSource="{Binding}" />
</Border>

This code sets a WorkspaceView.DataContextto a collection of UserControls. When you specify {Binding}whithin WorkspaceView's XAML you are refering to the whole DataContextobject (i.e. your collection.) This way you are setting the TabControl.ItemsSourceto your collection of views.

此代码将 a 设置WorkspaceView.DataContext为 的集合UserControls。当您指定{Binding}whithinWorkspaceView的 XAML 时,您指的是整个DataContext对象(即您的集合)。这样您就可以将 设置TabControl.ItemsSource为您的视图集合。

Now you could create DataTemplatestargeting the type of each view to control how each control is displayed in its tab within the TabControl.

现在,您可以创建DataTemplates针对每个视图类型的目标,以控制每个控件在TabControl.