视图未创建实例但视图未显示的MVVM问题 [英] MVVM issue with View not creating an instance but view not displaying

查看:121
本文介绍了视图未创建实例但视图未显示的MVVM问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我似乎看不到为什么不显示视图.我已经尝试过同时使用ContentControl和ContentPresenter.
如果我将数据模板添加到MakeControl(视图)中并绑定到Grid DataContext,它将起作用.我正在尝试从MainWindowViewModel进行绑定.

Hi
I can''t seem to see why the View is not displaying. I have tried using both the ContentControl and the ContentPresenter.
If I add the datatemplate within the MakeControl (view) and bind to the Grid DataContext it works. I am trying to bind from the MainWindowViewModel.

public MainWindowViewModel()
{
       MakeViewModel mMakeViewModel = new MakeViewModel();
       this.ViewModels .Add(mMakeViewModel);
}


<Window......

    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:MakeViewModel}">
            <vw:MakeControl/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Border >
            <ContentControl Content="{Binding ViewModels}" />
        </Border>
    </Grid>
<Window/>



窗口上的代码后面:



Behind Code on the Window:

base.OnStartup(e);
CallWindow mCallWindow = new CallWindow();
var mViewModel = new MainWindowViewModel();
mCallWindow.DataContext = mViewModel ;
mCallWindow.Show();

推荐答案

在这里不太了解您要实现的目标... ContentControl的ContentProperty绑定到某种MakeViewModel集合上,但是您仅针对MakeViewModel类型而不是其集合具有DataTemplate.如果试图显示MakeViewModel的集合,则应或定义DataTemplate以显示该集合(不仅是一项),或者使用ItemsControl而不是ContentControl. br/>
希望这会有所帮助,但如果没有帮助,请澄清问题,以便我进一步帮助您.

最好的问候.
劳尔·迈纳迪(Raul Mainardi Neto)
Not quite understand what you are trying to achieve here... Your ContentControl has its ContentProperty binded to a some kind of collection of MakeViewModel but you have the DataTemplate only for the type MakeViewModel, not its collection... If you are trying to show the collection of MakeViewModel you should or define the DataTemplate to show the collection (not only one item) or use ItemsControl instead of the ContentControl.

Hope this helps, but if does not, clarify the question so I can help you further.

Best regards.
Raul Mainardi Neto


这篇关于视图未创建实例但视图未显示的MVVM问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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