我想根據組合框選定的項目值動態加載視圖。我本週從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}" />
我希望能說清楚的人。
嗯,我有:RaisePropertyChanged(「SelectedItem」)在我的ViewModel,但是當我調試,似乎沒有發生=/ – 2014-12-19 10:43:41
好了,在瞭解了更多關於MVVM後,這就是答案。對於我來說,我太新手瞭解INotifyPropertyChanged和PropertyChanged是什麼。謝謝! – 2015-01-12 14:35:24