2014-12-19 119 views
0

我想根据组合框选定的项目值动态加载视图。我本周从MVVM开始,可能我没有看到任何东西。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做到这一点,如图所示。如果任何人都可以给我一些头了,我真的很感激它:)

约翰

编辑和实例:

好,因为查兰指出,我不得不使用很好的PropertyChanged 。

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

设置属性。

在这里,我创建了组合框的事件从它取决于该视图必须显示并设置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:

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

我希望能说清楚的人。

回答

1

您是否尝试过实施在你的视图模型INotifyPropertyChanged的,然后抬起的PropertyChanged事件时的SelectedItem被设置?

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

+0

嗯,我有:RaisePropertyChanged(“SelectedItem”)在我的ViewModel,但是当我调试,似乎没有发生=/ – 2014-12-19 10:43:41

+0

好了,在了解了更多关于MVVM后,这就是答案。对于我来说,我太新手了解INotifyPropertyChanged和PropertyChanged是什么。谢谢! – 2015-01-12 14:35:24