wpf 如何在 MainWindow 中显示我的用户控件?

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

How can I display my user control in the MainWindow?

wpfbindinguser-controls

提问by Garth Marenghi

I'm trying to build a small MVVM test application, but can't really figure how to display my user control in the MainWindow.

我正在尝试构建一个小型 MVVM 测试应用程序,但无法真正弄清楚如何在 MainWindow 中显示我的用户控件。

My Solution Explorer:

我的解决方案资源管理器:

How my solution looks like

我的解决方案看起来像

I got a resource dictionary:

我得到了一个资源字典:

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MVVM.ViewModel"
    xmlns:vw="clr-namespace:MVVM.View">

    <DataTemplate DataType="{x:Type vm:ViewModel}">
        <vw:View />
    </DataTemplate>

</ResourceDictionary>

I got my view:

我有我的看法:

<UserControl x:Class="MVVM.View.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate x:Key="PersonTemplate">
            <StackPanel>
                <TextBlock Text="{Binding FirstName}" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>

    <ListBox ItemsSource="{Binding Path=Persons}"
             ItemTemplate="{StaticResource PersonTemplate}" />
</UserControl>

and My MainWindow

和我的主窗口

<Window x:Class="MVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:MVVM.ViewModel"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary Source="MainWindowResources.xaml" />
    </Window.Resources>

    <Grid>

    </Grid>
</Window>

回答by vortexwolf

The most obvious and easiest way is to add the ContentControlelement:

最明显和最简单的方法是添加ContentControl元素:

<Grid>
    <ContentControl x:Name="mainContentControl" />
</Grid>

And after that set the Contentproperty of this control to your view model, and the corresponding view will be loaded and applied automatically:

然后Content将此控件的属性设置为您的视图模型,相应的视图将自动加载和应用:

this.mainContentControl.Content = new ViewModel.ViewModel();

But I would prefer to use another way without datatemplates:

但我更愿意使用没有数据模板的另一种方式:

<Grid>
    <vw:View x:Name="mainView"/>
</Grid>
this.mainView.DataContext = new ViewModel.ViewModel();

回答by Damascus

Build your VS2010 solution, then, go to your MainWindow's XAML.

构建您的 VS2010 解决方案,然后转到您的 MainWindow 的 XAML。

On the left, there is a toolbar with button "Toolbox"

在左侧,有一个带有“工具箱”按钮的工具栏

Open it, it contains all the possible WPF controls you could add to your UI

打开它,它包含您可以添加到 UI 的所有可能的 WPF 控件

Your UserControl should appear on top of the list (in a category probably named "MVVM Controls"), just drag&drop it to your UI :)

您的 UserControl 应该出现在列表的顶部(在一个可能名为“MVVM 控件”的类别中),只需将其拖放到您的 UI 中即可:)