将ListBox SelectedItems数据绑定到ViewModel

时间:2015-04-03 02:05:19

标签: c# wpf xaml data-binding listbox

我尝试使用我创建的附加属性对ListBox SelectedItems属性进行数据绑定。我建立了一个名为ListBoxFix的类,它位于一个名为ControlFixes的文件夹中。它的代码是一个非常简单的依赖属性,如下所示:

using System.Windows;
using System.Windows.Controls;

namespace QMAC.ControlFixes
{
    public static class ListBoxFix
    {
        public static bool GetSelectedItemsBinding(ListBox element)
        {
            return (bool)element.GetValue(SelectedItemsBindingProperty);
        }

        public static void SetSelectedItemsBinding(ListBox element, bool value)
        {
            element.SetValue(SelectedItemsBindingProperty, value);
            if (value)
            {
                element.SelectionChanged += (sender, args) =>
                {
                    var x = element.SelectedItems;
                };
            }
        }

        public static readonly DependencyProperty SelectedItemsBindingProperty =
            DependencyProperty.RegisterAttached("FixSelectedItemsBinding",
            typeof(bool), typeof(FrameworkElement), new PropertyMetadata(false));
    }
}

在我的XAML代码中,我有以下标记:

<Window x:Class="QMAC.MainWindow"
        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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
        xmlns:fix="clr-namespace:QMAC.ControlFixes"
        x:Name="Window"
        DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}"
        Title="QMAC" Width="554.779" ResizeMode="CanMinimize" Height="539" Icon="logo.ico" >
    <Grid Background="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" RenderTransformOrigin="0.593,0.948" Margin="0,0,0,1">

        <ListBox x:Name="schoolListBox" HorizontalAlignment="Left" Margin="25,86,0,0" Width="274" FontSize="16" SelectionMode="Extended" ItemsSource="{Binding LocationList}" fix:ListBox.SelectedItemsBindingProperty="true"  VerticalAlignment="Top" Height="364"></ListBox>
    </Grid>
</Window>

不幸的是,我在设置标记方面遇到了3个错误。他们是

Error   1   The name "ListBox" does not exist in the namespace "clr-namespace:QMAC.ControlFixes".
Error   2   The attachable property 'SelectedItemsBindingProperty' was not found in type 'ListBox'.
Error   3   The property 'ListBox.SelectedItemsBindingProperty' does not exist in XML namespace 'clr-namespace:QMAC.ControlFixes'.

我主要是想了解为什么它在我的ControlFixes命名空间中寻找ListBox?

1 个答案:

答案 0 :(得分:1)

您以错误的方式声明并使用附加属性。我建议你仔细阅读this well written overview

您的代码中存在以下错误:

  • 您所附属性的所有者类型被错误地指定为FrameworkElement
  • 注册的媒体资源名称与包含它的静态字段不匹配
  • 您尝试通过ListBox课程使用附加属性,尽管您已在ListBoxFix课程中对其进行了定义。

正确的附加属性定义应该类似于:

public static class ListBoxFix
{
    public static bool GetSelectedItemsBinding(ListBox element)
    {
        return (bool)element.GetValue(SelectedItemsBindingProperty);
    }

    public static void SetSelectedItemsBinding(ListBox element, bool value)
    {
        element.SetValue(SelectedItemsBindingProperty, value);
    }

    public static readonly DependencyProperty SelectedItemsBindingProperty =
        DependencyProperty.RegisterAttached("SelectedItemsBinding",
        typeof(bool), typeof(ListBoxFix), new PropertyMetadata(false));
}

请注意RegisterAttached()方法的 ownerType 参数提供包含附加属性的类的类型。看一下 name 参数。

正确使用附属物:

<ListBox fix:ListBoxFix.SelectedItemsBinding="true"/>

<强>更新

您可能希望以“WPF”样式使用附加属性。然后,最好将您的类设计为派生自DependencyObject。这就是MSDN所说的:

  

如果您的类严格定义附加属性以用于其他类型,则该类不必从DependencyObject派生。但是如果你遵循整体WPF模型,你需要从DependencyObject派生你的附加属性也是一个依赖属性。