您好我想用可观察集合的项目填充列表框。我有我的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");
}
}
但是列表框中没有填充任何数据。我有什么不对的,因为我无法找到它吗?
答案 0 :(得分:3)
我们在这里猜测,但它可以像这样简单吗?您必须绑定到属性(带有INotifyPropertyChanged)或DO中的依赖属性。
private ObservableCollection<string> something = new ObservableCollection<String> { "A", "B", "C", "D", "E", "F" };
public ObservableCollection<String> Something // Must be property or DP to be bound!
{
get { return something; }
set
{
if (Equals(value, something)) return;
something = value;
RaisePropertyChanged("Something");
}
}
如果没有完整的代码,很难说清楚。你是如何设置datacontext的?你为什么不直接绑定这个属性?
<ListBox ItemsSource="{Binding Something}"/>
启动应用程序时检查调试输出。
干杯
了Stian