作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试根据 ComboBox 选定项值动态加载 View 。我这周开始使用 MVVM,可能我什么也没看到。
组合框 View 位于上部,我希望下部 View 必须根据所选项目进行更改。
主视图如下所示:
<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");
}
}
// 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");
}
}
//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}" />
最佳答案
您是否尝试过实现 INotifyPropertyChanged 在您的 View 模型中,然后提高 属性已更改 时的事件已选项目 设置好了吗?
如果这本身不能解决它,那么您将能够手动提高 属性已更改 导航回页面时自己事件,这应该足以让 WPF 重绘自身并显示正确的选定项目。
关于c# - MVVM 根据 ComboBox 所选项目加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27563435/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!