- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 WinRT 应用程序,其中有一个带有组合框的 ListView 。Listview 有一个特定的 ObservableCollection 作为 Itemssource,ComboBox 应该有另一个 ObservableCollection 作为 ItemsSource,因为我应该能够动态更改 ComboBox 的内容。
我正在使用 MVVM-Light 框架,ObservableCollections 填充在 ViewModel 中并通过数据绑定(bind)显示。
我会给你一个示例 Xaml 代码:
<Page x:Class="MvvmLight2.MainPage"
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:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
d:DesignHeight="768"
d:DesignWidth="1366"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Page.Resources>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding CollectionOne}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding StringOne}"></TextBlock>
<ComboBox ItemsSource="{Binding CollectionTwo}" Width="500">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringTwo}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
和对应的ViewModel:
public class MainViewModel : ViewModelBase
{
private readonly IDataService _dataService;
public MainViewModel(IDataService dataService)
{
_dataService = dataService;
CollectionOne = new ObservableCollection<ClassOne>();
for (int i = 0; i < 4; i++)
{
var temp = new ClassOne()
{
StringOne = "String " + i.ToString()
};
CollectionOne.Add(temp);
}
CollectionTwo = new ObservableCollection<ClassTwo>();
CollectionTwo.Add(new ClassTwo("ADV"));
CollectionTwo.Add(new ClassTwo("Wettelijk"));
}
private ObservableCollection<ClassOne> _collectionOne;
public ObservableCollection<ClassOne> CollectionOne
{
get { return _collectionOne; }
set
{
if (_collectionOne == value)
{
return;
}
_collectionOne = value;
RaisePropertyChanged(() => CollectionOne);
}
}
private ObservableCollection<ClassTwo> _collectionTwo;
public ObservableCollection<ClassTwo> CollectionTwo
{
get { return _collectionTwo; }
set
{
if (_collectionTwo == value)
{
return;
}
_collectionTwo = value;
RaisePropertyChanged(() => CollectionTwo);
}
}
}
在 ClassOne 和 ClassTwo 中,例如,每个类中只有一个属性和一个字符串。
两个集合必须保持分开,因为随机填充时它们的长度可能不同。
编辑@Josh 我按照您的说明进行了操作,但它似乎仍然不起作用,这是我的调整:
<Page x:Class="MvvmLight2.MainPage"
x:Name="MyControl"
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:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
d:DesignHeight="768"
d:DesignWidth="1366"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Page.Resources>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding CollectionOne}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding StringOne}"></TextBlock>
<ComboBox ItemsSource="{Binding ElementName=MyControl, Path=CollectionTwo}" Width="500">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringTwo}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
最佳答案
由于您使用 ViewModel Locator 来设置数据上下文,因此您可以重复使用它来查找属性 CollectionTwo。
你的绑定(bind)看起来像这样:
<ComboBox ItemsSource="{Binding Path=Main.CollectionTwo, Source={StaticResource Locator}}" />
关于c# - 具有不同 ItemsSources 的 ListView 中的 WinRT MVVM-Light ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15479319/
是否可以在 WinRT 中实现自定义转换?例如。如果能够实现控件可见性的转换,那就太好了。因此,当您显示/隐藏分割 View 的一部分时,它会使用滑动效果为整个 View 添加动画效果。 最佳答案 是
我正在开发基于 XAML C# 的通用应用程序(目前专注于 WP8.1) 我的应用程序的主页非常复杂,有 3 个枢轴,每个枢轴都有网格、图像、边框、按钮、 ListView 等 问题有时是当我在执行其
我不久前从 C++/CX 切换到 C++/winrt,目前我被卡住了,因为我想创建一个单例 winrt 类。我阅读了有关 winrt::static_lifetime ( https://learn.
与 C++/CX 不同,C++/WinRT 中似乎没有 GUID 的包装器类型。它只使用普通的 C GUID struct原样。因此构造初始化 GUID 的唯一方法是使用 aggregate init
文档显示了这个 C# 片段: async void DisplayDeleteFileDialog(){ ContentDialog deleteFileDialog = new Conten
文档显示了这个 C# 片段: async void DisplayDeleteFileDialog(){ ContentDialog deleteFileDialog = new Conten
这是 xaml 的样子:
我有一个文本框,我不希望用户粘贴到其中。 如何防止粘贴? 从另一个应用程序中将文本拖放到 TextBox 中也是一个粘贴事件吗? 最佳答案 从 Windows 8.1 开始,Windows.UI.XA
这实际上更像是一个好奇心类型的问题。我负责跨程序集和原始 IL 查找类型。不过,Windows 8 确实让我正在实现的某些事情陷入困境。我发现 WinRT 和非 WinRT 框架程序集共享相同的完全限
我正在尝试创建一个条目页面,其中一个选项是选择一个项目。该列表可以超过 1000 个,并且显示列出项目的启用搜索的页面是有意义的。当用户从编辑/创建屏幕中单击“选择项目”时,我可以将导航参数传递到该屏
我正在将我的库移植到 WinRT,它依赖于 System.Globalization.Calendar 类及其派生类,如 GregorianCalendar 和 HijriCalendar 等。虽然这
我需要下载一个大的视频文件并将其保存到硬盘上。 然后我需要使用 XAML 媒体元素播放这个视频文件。 但该文件必须使用 AES 256 算法和加密 key 进行加密。任何时候都不得将未加密的数据写入硬
假设我有这样的事情: 像这样: public class MyViewModel : INotifyPropertyChanged { public MyViewModel() {
我正在尝试使用 WinRT 中的当前文化来格式化日期时间值。但是CurrentCulture 属性似乎并不尊重系统文化。 我尝试了以下两个属性, System.Globalization.Cultur
我的应用程序是在 silverlight 中开发的。计划在今年晚些时候推出。我担心我的 xaps 会被逆向工程。我的 wcf 服务确实有很多智能,但您不能将所有内容都放在服务中。现在 winrt 是另
我使用下面的代码发送电子邮件。但是当我在 VS 的模拟器中运行它时,无法启动电子邮件客户端。我做错了什么?但是电子邮件客户端以本地计算机模式启动。 var mailto = new Uri("mail
我正在开发通用应用程序。在为Windows Phone 8.1项目设计 View 时,不能使任何按钮的宽度小于109。如果将按钮的width属性设置为小于该宽度,则呈现时它将变为109。 我想知道如何
情况: 我从不同的互联网位置获取 json 对象。 这些包含我放在 BingMap 上的许多地理坐标。效果非常好。 问题: 但是当我从互联网位置获取数据时,我得到了一个阻塞的用户界面。有没有办法在后台
想法是这样的,您正在 Visual Studio 2013 Update 2 中为 Windows 8.1-Update 和 Windows Phone 8.1 创建一个 WinRT-XAML 通用应
什么是更新(替换)本地存储中的图像的好方法,该图像绑定(bind)到导航堆栈中的图像控件。 更具体地说:我在页面 A (ViewProfile.xaml) 中显示图像,图像源绑定(bind)到属性 I
我是一名优秀的程序员,十分优秀!