MVVM 根据组合框所选项加载视图

本文关键字:选项 加载 视图 组合 MVVM | 更新日期: 2023-09-27 18:34:45

我正在尝试根据组合框选定的项目值动态加载视图。我本周开始使用 MVVM,可能我没有看到什么。

组合框视图位于上部,我希望下部视图必须根据所选项目进行更改。

主视图如下所示:

   <UserControl.Resources>
    <swv:SelectSolidWorkFileTypeView x:Key="Selector" />
    <DataTemplate DataType="{x:Type swv:SelectSolidWorkFileTypeView}"  >
        <swv:SolidWorkAssembliesFilesView />
    </DataTemplate>
    <swv:SolidWorkAssembliesFilesView x:Key="AssemblyFilesView" />
    <DataTemplate DataType="{x:Type swv:SolidWorkAssembliesFilesView}">
        <swv:SolidWorkAssembliesFilesView />
    </DataTemplate>
    <swv:SolidWorksRotorFilesView x:Key="RotorenFilesView" />
    <DataTemplate DataType="{x:Type swv:SolidWorksRotorFilesView}">
        <swv:SolidWorkAssembliesFilesView />
    </DataTemplate>
</UserControl.Resources>
<Grid Background="Black">
    <Grid.RowDefinitions>
        <RowDefinition Height="10"/>
        <RowDefinition Height="230"/>
        <RowDefinition Height="6"/>
        <RowDefinition Height="100*"/>
        <RowDefinition Height="10"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="100*"/>
        <ColumnDefinition Width="10"/>
    </Grid.ColumnDefinitions>
    <ContentControl Content="{StaticResource Selector}" Grid.Column="1" Grid.Row="1" />
    <ContentControl Content="{Binding Content}" Grid.Column="1" Grid.Row="3" />

我从列表对象加载,组合框的值

模型视图是(我认为是相关的(:

// Property to embed views on the main view
    object _content;
    public object Content
    {
        get { return _content; }
        set
        {
            _content = value;
            RaisePropertyChanged("Content");
        }
    }
    List<string> _source = new List<string> { "Assemblies", "Rotoren" };
    string _selectedItem = null;
    //property to return items to the view
    public List<string> Source { get { return _source; } }
    //property to hold the selected item
    public string SelectedItem 
    { 
        get 
        { 
            return _selectedItem;
        } 
        set 
        { 
            _selectedItem = value; RaisePropertyChanged("SelectedItem"); 
        } 
    }

我一直在寻找如何做到这一点的例子,但我没有运气,顺便说一句,我想用 ContentControl 制作它,如图所示。如果有人能给我一些提示,我将不胜感激:)

John

编辑和示例:

好吧,正如Charan指出的那样,我只需要很好地使用PropertyChanged。

当我使用MVVM Light Toolkit时,我使用RisePropertyChanged。我所做的是...

设置属性。

在这里,我为 ComboBox 创建了一个事件,因为它取决于必须显示哪个视图并设置 CurrentView 属性:

    // cbType is a ComboBox, here is the property to it
    private string _cbType;
    public string cbType
    {
        get { return _cbType; }
        set
        {
            _cbType = value;
            if (_cbType == "Assemblies")
                //if the Type is Assemblies, it will call the proper view for it
                CurrentViewModel = new SolidWorkAssembliesFilesView();
            if (_cbType == "Rotoren")
                //if the Type is Rotoren, it will call the proper view for it
                CurrentViewModel = new SolidWorksRotorFilesView();
            RaisePropertyChanged("cbType");
        }
    }

CurrentViewModel也是我创建的属性,因此,当它发生变化时,将触发事件并更改视图。

    //Nothing special here
    private object currentViewModel;
    public object CurrentViewModel
    {
        get { return currentViewModel; }
        set
        {
            currentViewModel = value;
            RaisePropertyChanged("CurrentViewModel");
        }
    }

最后,您只需正确绑定属性,在本例中为视图中的组合框:

<ComboBox Grid.Column="1" Text="{Binding Path=cbType, Mode=TwoWay}" ItemsSource="{Binding Path=Source}" />

我希望它能让某人清楚。

MVVM 根据组合框所选项加载视图

您是否尝试过在视图模型中实现 INotifyPropertyChanged,然后在设置选定项时引发 PropertyChanged 事件?

如果这本身不能修复它,那么您将能够在导航回页面时自行手动引发 PropertyChanged 事件,这应该足以让 WPF 重新绘制自身并显示正确的选定项。

相关文章: