2015-04-03 51 views
0

我试图绑定ListBox SelectedItems属性使用我创建的附加属性。我设置了一个名为ListBoxFix的类,该类位于名为ControlFixes的文件夹中。它的代码如下所示一个非常简单的依赖属性:数据绑定列表框SelectedItems ViewModel

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

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

有在你的代码如下错误:

  • 您的附加属性的所有者类型被错误地指定为FrameworkElement
  • 注册的属性名称不匹配包含它
  • 您尝试,虽然你已经在你的ListBoxFix类中定义它通过ListBox类使用附加属性的静态字段。

适当的附加属性定义应类似于此:

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参数提供你的类包含附加属性的类型。看看名称参数。

您的附加属性的正确用法:

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

更新:

你可能想使用你的附加属性在 “WPF” 的风格。那么最好将你的课程设计成DependencyObject。这是MSDN所说的:

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

+0

我已经将修复指向了我的正确名称空间,但它仍然告诉我在该名称空间中找不到ListBoxFix。 – tylerbhughes 2015-04-03 17:55:22

+0

@RandomlyKnighted,有时可能是由IntelliSence引起的。尝试清理并重建项目或事件关闭Visual Studio并重新启动它。我在VS中检查过我的代码,它工作正常。 – dymanoid 2015-04-03 18:46:40

+0

为了清楚你的更新,因为我的ListBoxFix类仅用于添加依赖属性的目的,我的类将需要继承DependencyObject的属性? – tylerbhughes 2015-04-05 03:47:08