- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一组模型(即格式类),我想将其加载到我的 View (即 MainWindowView.xaml)中。这些模型可以加载多次。
为了做到这一点,我开始为每个模型类创建一个 ViewModel
和一个 DataTemplate
。例如,模型类 FormatContainer
有 FormatContainerViewModel
作为 View 模型:
namespace MyProject.ViewModels
{
public class FormatContainerViewModel : ViewModelBase
{
public FormatContainerViewModel(FormatContainer wrappedFormat)
{
_wrappedFormat = wrappedFormat; // Wrap model.
SubFormats = GetSubFormatsViewModels(_wrappedFormat.SubFormats); // Get the collection of Formats contained into container and generate their ViewModels.
FormatLabel = _wrappedFormat.Description; // Get wrapped format description.
}
private FormatContainer _wrappedFormat;
private ObservableCollection<ViewModel> _subFormats;
public ObservableCollection<ViewModel> SubFormats
{
get
{
return _subFormats;
}
set
{
Set(() => SubFormats, ref _subFormats, value);
RaisePropertyChanged("SubFormats");
}
}
private string _formatLabel;
public string FormatLabel
{
get
{
return _formatLabel;
}
set
{
Set(() => FormatLabel, ref _formatLabel, value);
RaisePropertyChanged("FormatLabel");
}
}
}
}
此外,模型 FormatContainer 有一个 DataTemplate 作为 View ,由 Resourcedictionary 添加:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.ViewModels">
<DataTemplate DataType="{x:Type local:FormatContainerViewModel}">
<Border BorderBrush="Black" BorderThickness="1" Padding="20">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<!-- Show container description. -->
<TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding FormatLabel}" VerticalAlignment="Center" Margin="5" />
<!-- Show container sub formats. -->
<!--<StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Margin="3,3,3,3">
<ContentControl Content="{Binding SubFormats}" HorizontalAlignment="Center" Margin="1" />
</StackPanel-->>
</Grid>
</Border>
</DataTemplate>
<!-- other DataTemplates ... -->
</ResourceDictionary>
因此,在 App.xaml 中添加了这个资源字典(即 FormatsView.xaml):
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject"
xmlns:vm="clr-namespace:MyProject.ViewModels"
StartupUri="MainWindowView.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:MyProject.ViewModels" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PanelsView.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
然后,这些 ViewModel 由服务类(即 ViewModelsGeneratorService)在主视图模型(即 MainWindowViewModel)中实例化。
ViewModelsGeneratorService tempGenerator = new ViewModelsGeneratorService(myFormats); // Use a service to convert current format objects to view models.
ViewModelsList = tempGenerator.ViewModelsList; // Populate view models obesrvable collections.
最后,这些 View 模型通过 ContentControl 在 MainWindowView 中显示:
<Window x:Class="MyProject.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
xmlns:interactivity="using:Microsoft.Expression.Interactivity"
xmlns:core="using:Microsoft.Expression.Interactions.Core"
mc:Ignorable="d"
Title="MySoftware" Height="350" Width="525"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<!-- ... -->
<ContentControl Grid.Row="3" Content="{Binding ViewModelsList}" />
</Grid>
</Window>
不幸的是,包含内容 ViewModelsList 的 ContentControl 是空的,而 ViewModelLists 包含 53 个项目。我错过了什么?请问有人可以指点一下吗?
我已经检查了一些教程和讨论以检查我的代码(例如 Rachel 、 Gala 、 Marco 等),但我无法弄清楚我缺少什么..
此外,ContentControl 不显示子格式。当然,当未注释时 ;-) 我在这里还缺少什么?
祝一切顺利,洛伦佐
附言我目前正在为 .NET 4.5 开发并使用 MVVM Light 框架。
附言FormatContainer 是我的格式中唯一具有子格式的格式,其他格式只是更改它们的属性,因此它们的 View (例如,除了标签之外,格式可以显示组合框而不是列表框,和/或而不是文本框,等)。
最佳答案
集合受 ItemsControl
或其他基于列表的控件的约束,而不是受 ContentControl
的约束。
<ItemsControl ItemsSource="{Binding ViewModelsList}"/>
如果您想要一个不继承ItemsControl
(如WrapPanel
)的ItemsPanel
,您可以使用ItemsPanelTemplate 进行设置
属性。
关于c# - 为什么这个 ViewModels 的 ObservableCollection 没有通过使用 DataTemplates 显示在 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38326576/
Glenn Block 和我一直在研究 ViewModel 模式。我们一直在尝试找出与该模式相关的最大痛点,目标是添加框架支持以减轻痛苦。 今晚,格伦发帖,“View Model” – the mov
如果我使用 Views 创建 ViewModel 实例且 ViewModel 没有对 View 的引用的方法,我不知道创建 viewmodel 关系的正确方法是什么。 假设我们有 ChildView
当从缓存中重新加载 ViewModel 时,我需要能够拦截框架并执行重新初始化。由于没有重新创建 ViewModel,我既不能使用 Init()、MvxViewModel.InitFromBundle
我的业务模型(实际上它用于使用 Entity Framework 6 读取数据)看起来像: class Profile : NameDescriptionBase { public virtu
我在swift中玩MVVM遇到了这种情况:我为 tableView 创建了模型,其中包含对象列表和对象计数。有点像 class TableViewViewModel { var count :
我正在研究由以下部分组成的应用程序区域: Explorer - 包含 TreeView PropertyInspector - 包含一个 PropertyGrid 编辑器 - 包含一个 Explore
我正在使用 MVVM-Light,并且我有一个列出了销售人员的 DataGrid 工作。用户可以双击打开一个子窗口,该窗口将在网格上列出他们的销售,用户将能够在该网格下填写一些文本框以添加新的销售。
是否有适当的方法来创建包含 subViewModel 的 C#/WPF ViewModel ? 目标是: 我有一个主窗口。该窗口用于读取/创建图像。窗口上有一个按钮,可以在 2 个 UserContr
首先,如果这很简单,我必须道歉。我对 WPF 和 MVVM 非常陌生,我想确保我没有违反任何 WPF 或 MVVM 概念。此外,对于下面的冗长解释(试图提供所有细节): 我目前正在引用具有所有业务逻辑
我有一个架构问题,以及一个我想提出意见的可能解决方案。 我习惯于 WP7 的 MVVM 架构(尽可能但不幸的是,有时 sdk 似乎朝着相反的方向发展)。 WP7 强制采用 ViewFirst 方法,我
鉴于以下情况: ViewModelA 启动 ViewModelB(当然,通过一个通用 Controller ,它使用 Ioc 和 DI 来解析所需的类型)。 ViewModelB 需要在 ViewMo
在我的 WPF MVVM 应用程序中,使用 Caliburn.Micro,我有一个 ViewModel,CreateServiceViewModel,在单击按钮时,它会在单独的窗口中打开一个 Grid
假设我有一个采用特定 ViewModel 的页面( View ): @model IEnumerable 在这个页面中,我有一个通过另一个 ViewModel(我们称之为 PostModel)发布数据
我有两个相似的 ViewModel,我需要将一个转换为另一个。 这是第一个: using System; using System.Collections.Generic; using System.
我有两个 View 共享来自某个 View 模型的一个可观察集合,但具有不同的 Collection View 参数。在 MVVM Light 中实现它的正确方法是什么?是否支持非静态虚拟机?我如何管
我有 2 个 View 模型 - 主视图模型** 存储 View 模型 StorageViewModel.kt class StorageViewModel @ViewModelInject cons
我有一个 AddressesViewModel 保存用户最喜欢的地址,另一个 SearchViewModel 保存搜索到的地址。当用户搜索地址时,我必须通过检查收藏夹数组来检查该地址是否是收藏夹。正确
首先,我看过this post并没有找到我的问题的答案。 我不确定这是否是汇总 型号 类或聚合 查看型号 类,但这就是我所拥有的: 在我的 WPF(使用 Prism)应用程序中,我有一个 View “
我的 View 中有这些过滤器,它们都会更新 FilterViewModel,然后由它负责过滤数据。其中一个 View ,SearchAddressView 需要 PlacemarkViewModel
依赖的 ViewModel 通过构造函数(IoC 容器)注入(inject)。 示例:ProductSelectionViewModel 使用 ShoppingBasketViewModel。 这是一
我是一名优秀的程序员,十分优秀!