gpt4 book ai didi

c# - MVVM 根据 ComboBox 所选项目加载 View

转载 作者:行者123 更新时间:2023-12-03 10:36:11 25 4
gpt4 key购买 nike

我正在尝试根据 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" />

我从 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 也是我创建的一个属性,因此,当它发生变化时,将触发事件并更改 View 。
    //Nothing special here
private object currentViewModel;
public object CurrentViewModel
{
get { return currentViewModel; }
set
{
currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}

最后,您只需要正确绑定(bind)属性,在这种情况下, View 中的组合框:
<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com