简体   繁体   English

Silverlight用户控件数据绑定

[英]Silverlight user control databinding

I am trying to create a user control that contains a list box and I can't figure out how to properly setup the databinding. 我试图创建一个包含列表框的用户控件,但我不知道如何正确设置数据绑定。

In the MainForm.xaml (MyItems is a ObservableCollection defined in the ViewModel): 在MainForm.xaml中(MyItems是在ViewModel中定义的ObservableCollection):

<my:ItemsList Items="{Binding MyItems}"/>

The user contol: 用户控制:

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);
}

And the xaml (namespaces declarations omitted): 和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>

The error I'm getting: BindingExpression path error: 'Items' property not found on 'MyApp.ViewModels.MainViewModel' ?!? 我得到的错误:BindingExpression路径错误:在'MyApp.ViewModels.MainViewModel'上找不到'Items'属性?

我所缺少的是在用户控件的构造函数中为列表框设置数据上下文...

LayoutRoot.DataContext = this;

Check: do you use correct DataContext of your page(must be your ViewModel)? 检查:您是否使用页面的正确DataContext(必须为ViewModel)?

The user contol must be: 用户控制必须是:

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), new PropertyMetadata(ItemsChanged));

    private static void ItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var controll = (ItemsList)d;
        var val = (IEnumerable)e.NewValue;
        controll.lls.ItemSource = val;
    }

Xaml Xaml

<UserControl x:Class="MyApp.Controls.ItemsList">
    <phone:LongListSelector x:name="lls">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding ItemName}" />
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</UserControl>

hope its help 希望它的帮助

In your xaml you need to add a reference to the viewmodel and make it the datacontext for the control. 在您的xaml中,您需要添加对viewmodel的引用,并使它成为控件的datacontext。

    <UserControl xmlns:local="clr-namespace:"myproject.mynamespace;assembly=myproject">
        <UserControl.Resources>
           <local:myviewmodel x:key="viewModel"/>
        </UserControl.Resources>
        <UserControl.DataContext>
           <Binding Source="{StaticResource viewModel}"/>
        </UserControl.DataContext>
        <phone:LongListSelector ItemsSource="{Binding Items}">
           <phone:LongListSelector.ItemTemplate>
           <DataTemplate>
             <TextBlock Text="{Binding ItemName}" />
          </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
       </phone:LongListSelector>
    </UserControl>

Just a note: You can use the DisplayMemberPath="ItemName" attribute instead of a data template unless you need to interact with the textblock in some way. 请注意:可以使用DisplayMemberPath =“ ItemName”属性而不是数据模板,除非您需要以某种方式与文本块进行交互。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM