2015-04-03 50 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