【问题标题】:MVVM Load views depending on ComboBox selected itemMVVM 根据 ComboBox 所选项目加载视图
【发布时间】:2015-02-18 05:38:46
【问题描述】:

我正在尝试根据 ComboBox 选定项的值动态加载视图。我这周开始使用 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" />

我从 List 对象加载 ComboBox 的值

ModelView 是(我认为相关的):

// 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 制作它,如图所示。如果有人能给我一些提示,我将不胜感激:)

约翰

编辑和示例:

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

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

设置属性。

在这里我为 ComboBox 创建了一个事件,因为它取决于必须显示哪个 View 并设置 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 也是我创建的一个 Property,所​​以当它发生变化时,将触发事件并更改 View。

    //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}" />

我希望它可以让某人清楚。

【问题讨论】:

    标签: c# mvvm view combobox viewmodel


    【解决方案1】:

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

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

    【讨论】:

    • 好吧,我的 ViewModel 中有:RaisePropertyChanged("SelectedItem"),但是当我调试时,似乎什么也没发生 =/
    • 好吧,在了解了更多关于 MVVM 的知识之后,这就是答案。我对我来说太新手了,无法理解 INotifyPropertyChanged 和 PropertyChanged 是什么。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多