我怎样才能显示在主窗口我的用户控制? [英] How can I display my user control in the MainWindow?

查看:149
本文介绍了我怎样才能显示在主窗口我的用户控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个小型MVVM测试应用程序,而不能真正弄清楚如何显示在主窗口我的用户控制。

我的解决方案管理器:

我有资源字典:

 < ResourceDictionary中
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:VM =CLR的命名空间:MVVM.ViewModel
    的xmlns:VW =CLR的命名空间:MVVM.View>    <数据类型的DataTemplate ={X:类型VM:视图模型}>
        <大众:查看/>
    < / DataTemplate中>< / ResourceDictionary的>

我得到了我的看法:

 <用户控件X:类=MVVM.View.View
             的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
             的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
             的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
             的xmlns:D =htt​​p://schemas.microsoft.com/ex$p$pssion/blend/2008
             MC:可忽略=D
             D:DesignHeight =300D:DesignWidth =300>
    < UserControl.Resources>
        <的DataTemplate X:键=PersonTemplate>
            <&StackPanel的GT;
                < TextBlock的文本={结合姓}/>
            < / StackPanel的>
        < / DataTemplate中>
    < /UserControl.Resources>    < ListBox中的ItemsSource ={绑定路径=人士}
             的ItemTemplate ={StaticResource的PersonTemplate}/>
< /用户控件>

和我的主窗口

 <窗​​口x:类=MVVM.MainWindow
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        的xmlns:VM =CLR的命名空间:MVVM.ViewModel
        标题=主窗口HEIGHT =350WIDTH =525>
    < Window.Resources>
        < ResourceDictionary中源=MainWindowResources.xaml/>
    < /Window.Resources>    <网格和GT;    < /网格和GT;
< /窗GT;


解决方案

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

 <网格和GT;
    < ContentControl中X:NAME =mainContentControl/>
< /网格和GT;

而在此之后将内容此控制您的视图模型属性和相应的视图会被加载,并自动应用:

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

不过,我想preFER用另外的方式没有的DataTemplates:

 <网格和GT;
    < VW:查看X:NAME =MAINVIEW/>
< /网格和GT;
this.mainView.DataContext =新ViewModel.ViewModel();

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

My Solution Explorer:

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>

解决方案

The most obvious and easiest way is to add the ContentControl element:

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

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

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();

这篇关于我怎样才能显示在主窗口我的用户控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆