我正在尝试创建一个包含列表框的用户控件,但我不知道如何正确设置数据绑定。
在 MainForm.xaml 中(MyItems 是 ViewModel 中定义的 ObservableCollection):
<my:ItemsList Items="{Binding MyItems}"/>
用户控制:
public partial class ItemsList : UserControl
{
public ItemsList()
{
InitializeComponent();
}
public IEnumerable Items
{
get { return (IEnumerable)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(IEnumerable), typeof(ItemsList), null);
}
和 xaml(省略命名空间声明):
<UserControl x:Class="MyApp.Controls.ItemsList">
<phone:LongListSelector ItemsSource="{Binding Items}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}" />
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</UserControl>
我得到的错误:BindingExpression 路径错误:在“MyApp.ViewModels.MainViewModel”上找不到“Items”属性?!?