【发布时间】:2014-08-22 08:31:31
【问题描述】:
嗨,我想用可观察集合的项目填充列表框。我的 XAML 文件中有:
<catel:UserControl x:Class="Musat.Classificator.CatelMVVM.Views.StopControlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:catel="http://catel.codeplex.com"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<catel:UserControl.Resources>
<CollectionViewSource Source="{Binding Something}" x:Key="cvsStops" />
</catel:UserControl.Resources>
<catel:StackGrid x:Name="LayoutRoot">
<ListBox ItemsSource="{Binding Source={StaticResource cvsStops}}" />
</catel:StackGrid>
在我的 ViewModel 中我有:
class StopViewModel : ViewModelBase
{
public StopViewModel()
{
ObservableCollection<String> Something = new ObservableCollection<String>();
Something.Add("A");
Something.Add("B");
Something.Add("C");
Something.Add("D");
Something.Add("E");
Something.Add("F");
}
}
但是列表框没有填充任何数据。有没有因为找不到而做错了什么?
【问题讨论】:
-
为什么要通过静态资源绑定而不是直接绑定?
-
还有一点,catel:UserControl的DataContext在哪里设置
-
我不认为这是问题
-
Something 是属性吗?一个DP?它是否实现了 INotiftyPropertyChanged?请发布完整的代码。
-
@user3182266 目前
Something是构造函数的局部变量,它需要是StopViewModel的公共属性,StopControlView的DataContext也必须相应设置