与列表框和家长控制用户控件之间的绑定(MVVM) [英] Binding between Usercontrol with listbox and parent control (MVVM)

查看:103
本文介绍了与列表框和家长控制用户控件之间的绑定(MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个列表框和几个按钮一个用户控件。

I have a UserControl which contains a listbox and few buttons.

<UserControl x:Class="ItemControls.ListBoxControl"
    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">
    <Grid>
        <ListBox:ExtendedListBox SelectionMode="Single" ItemsSource="{Binding LBItems}" Height="184">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
        </ListBox>
<Button Command="RemoveCommand"/>
</Grid>
</UserControl>

和背后的code:

public static readonly DependencyProperty RemoveCommandProperty =
 DependencyProperty.Register("RemoveCommand", typeof(ICommand), typeof(ListBoxControl), null);

 public ICommand RemoveCommand
 {
  get { return (ICommand)GetValue(RemoveCommandProperty); }
  set { SetValue(RemoveCommandProperty, value); }
 }

 public static readonly DependencyProperty LBItemsProperty =
 DependencyProperty.Register("LBItems", typeof(IEnumerable), typeof(ListBoxControl), null);

 public IEnumerable LBItems
 {
  get { return (IEnumerable)GetValue(LBItemsProperty); }
  set { SetValue(LBItemsProperty, value); }
 }

我使用这个控制这样的观点:

I'm using this control in the view like this:

<ItemControls:ListBoxControl Height="240" Width="350" LBItems="{Binding Items, Converter={StaticResource ItemsConverter}, Mode=TwoWay}" RemoveCommand="{Binding RemoveCommand}"/>

该命令工作正常,虽然列表框绑定不。我的问题是 - 为什么?

The command works fine, though the listbox binding doesn't. My question is - WHY?

推荐答案

在你的用户控件列表框不正确绑定到LBItems。 ListBox的DataContext的是不是你的控制,因此它试图直接从您的视图模型绑定LBItems。

The ListBox in your UserControl isn't correctly binding to LBItems. The DataContext of the ListBox is not your control so it's trying to bind LBItems directly from your ViewModel.

在你的用户控件声明中加入的DataContext ={绑定的RelativeSource = {自我的RelativeSource}}。这应该您的DataContext正确设置到用户控件,并允许您绑定到正确定位LBItems属性。

In your UserControl declaration add DataContext="{Binding RelativeSource={RelativeSource Self}}". That should correctly set your DataContext to the UserControl and allow you binding to correctly locate the LBItems property.

修改

您的评论提醒了我。您需要设置你的网格的DataContext的是你的用户控件。要做到这一点最简单的方法是命名网格即&LT;电网X:NAME =LayoutRoot&GT; ,然后在构造你的用户控件 LayoutRoot.DataContext =这一点;

Your comment reminded me. You need to set the DataContext of your Grid to be your UserControl. The simplest way to do this is to name the Grid i.e. <Grid x:Name="LayoutRoot"> and then in the constructor for your UserControl LayoutRoot.DataContext = this;

如果您设置的用户控件的您从VM打破绑定,但如果你将它们设置在网格上绑定仍然工作和用户控件内的所有控件可以正确地绑定到用户控件。DataContext的

If you set the DataContext of the UserControl you break the bindings from your VM, but if you set them on the Grid the top bindings still work and all controls inside the UserControl can correctly bind to the UserControl.

这篇关于与列表框和家长控制用户控件之间的绑定(MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆