如何在XML中将同一视图模型与不同视图一起使用-WPF MVVM

时间:2019-07-14 06:20:15

标签: wpf mvvm

我想做的是这样的:

 <UserControl.Resources>       
        <DataTemplate DataType="{x:Type local:GraphXYLineViewModel}" >            
            <local:GraphXYLineView />
        </DataTemplate>


 </UserControl.Resources>

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />

        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Visibility="{Binding IsNotFreezeViewer, Converter={local:BoolToVisibleCollapsedConverter}}" >
            <ContentControl Content="{Binding GraphXYLineViewModel}" />
        </Grid>
        <Grid Grid.Column="0" Visibility="{Binding IsFreezeViewer, Converter={local:BoolToVisibleCollapsedConverter}}" >
            <ContentControl Content="{Binding GraphXYLineFreezeViewModel}" />
        </Grid>
    </Grid>

我想要2个视图来查看有时GraphXYLineViewModel和有时GraphXYLineFreezeViewModel, 但是当我尝试显示第二个视图时,我什么也没得到,是因为它是相同类型的视图模型吗?

在视图模型中,它们看起来像这样:

    GraphXYLineViewModel _graphXYLineVM;
    GraphXYLineViewModel _graphXYLineFreezeVM;

    public IPageViewModel GraphXYLineViewModel
        {
            get{
                return (IPageViewModel)_graphXYLineVM;
            }
        }

    public IPageViewModel GraphXYLineFreezeViewModel
    {
        get{
            return (IPageViewModel)_graphXYLineFreezeVM;
        }
    }

我如何将其设置为工作状态?

2 个答案:

答案 0 :(得分:0)

<ContentPresenter Content="{Binding GraphXYLineViewModel}">
    <ContentPresenter.Style>
        <Style TargetType="ContentPresenter">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <local:GraphXYLineView />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsFreezeViewer}" Value="True">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <local:GraphXYLineFreezeView />
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentPresenter.Style>
</ContentPresenter>

或者仅编写一个DataTemplateSelector

答案 1 :(得分:0)

当您有两个相同类型的视图模型实例并且想要在同一视图中显示一个或另一个时,您不需要两个视图模型属性。

拥有一个包含一个或另一个视图模型实例的属性似乎就足够了。但是,它必须触发属性更改通知才能更新视图(例如INotifyPropertyChanged接口的PropertyChanged事件):

private IPageViewModel graphXYLineViewModel;

public IPageViewModel GraphXYLineViewModel
{
    get { return graphXYLineViewModel; }
    set
    {
        graphXYLineViewModel = value;

        PropertyChanged?.Invoke(this,
            new PropertyChangedEventArgs(nameof(GraphXYLineViewModel)));
    } 
}

更改属性的代码如下:

GraphXYLineViewModel = IsFreezeViewer ? _graphXYLineFreezeVM : _graphXYLineVM;

由于您的UserControl仅包含

<ContentControl Content="{Binding GraphXYLineViewModel}" />

如果根本不需要它,这似乎也值得怀疑。