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

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

问题描述

我有一个 UserControl,其中包含一个列表框和几个按钮.

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>

以及背后的代码:

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?

推荐答案

您的 UserControl 中的 ListBox 未正确绑定到 LBItem.ListBox 的 DataContext 不是您的控件,因此它试图直接从您的 ViewModel 绑定 LBItem.

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.

在您的 UserControl 声明中添加 DataContext="{Binding RelativeSource={RelativeSource Self}}".这应该正确地将您的 DataContext 设置为 UserControl 并允许您绑定以正确定位 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.

编辑

你的评论提醒了我.您需要将 Grid 的 DataContext 设置为您的 UserControl.最简单的方法是命名网格,即 <Grid x:Name="LayoutRoot"> 然后在您的 UserControl 的构造函数中 LayoutRoot.DataContext = this;代码>

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;

如果你设置了 UserControl 的 DataContext,你会破坏 VM 的绑定,但是如果你在 Grid 上设置它们,顶部绑定仍然有效,UserControl 内的所有控件都可以正确绑定到 UserControl.

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天全站免登陆