将ListBox SelectedItems绑定到ViewModel

pallasmedia

我试图使用我创建的附加属性对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?

动力类

您以错误的方式声明和使用附加属性。我建议您仔细阅读这份写得很好的概述

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

  • 您附加属性的所有者类型错误地指定为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));
}

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

您附属财产的正确用法:

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

更新:

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

如果您的类严格定义要在其他类型上使用的附加属性,则该类不必从DependencyObject派生。但是,如果您遵循将附加属性也作为依赖项属性的整体WPF模型,则确实需要从DependencyObject派生。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

数据绑定ListBox SelectedItems属性

来自分类Dev

将 ViewModel 绑定到 CustomControl

来自分类Dev

将ListBox.SelectedItem绑定到属性

来自分类Dev

将List <Class>绑定到ListBox

来自分类Dev

WPF数据将ViewModel属性绑定到用户控件内部的ListBox中,背后的代码

来自分类Dev

将模型绑定到ViewModel(WPF)

来自分类Dev

将命令绑定到子ViewModel

来自分类Dev

将AngularJS绑定到MVC ViewModel属性

来自分类Dev

如何将TreeView绑定到ViewModel?

来自分类Dev

将事件绑定到ViewModel:扩展文章

来自分类Dev

将selectlistitems绑定到ViewModel的正确方法

来自分类Dev

将ComboBox绑定到Model / ViewModel

来自分类Dev

将ViewModel绑定到多个窗口

来自分类Dev

将UserControl的DataContext绑定到子ViewModel

来自分类Dev

无法将ViewModel绑定到View

来自分类Dev

使用敲除将列表绑定到Viewmodel

来自分类Dev

将标签绑定到ViewModel属性

来自分类Dev

将 Model 绑定到 ViewModel 的方法

来自分类Dev

尝试将商店绑定到 ViewModel

来自分类Dev

如何将ListBox绑定到列表并绑定int?

来自分类Dev

无法将ListView.SelectedItems.Count绑定到Button.IsEnabled

来自分类Dev

将Winforms ListBox集合绑定到List <object>

来自分类Dev

为什么将ObservableCollection <string>绑定到Listbox无效?

来自分类Dev

如何将字符串的observableCollection绑定到listBox

来自分类Dev

将DataGrid绑定到ListBox所选项目

来自分类Dev

将DataGrid绑定到ObservableCollection,后者是ListBox的SelectedItem的属性

来自分类Dev

如何将mousewheel绑定到tk inter _listbox?

来自分类Dev

如何将字符串的observableCollection绑定到listBox

来自分类Dev

将UserControl的ListBox的ItemSource绑定到父DataContext时出错

Related 相关文章

热门标签

归档