2011-10-19 67 views
0

我看到一個WPF中的一個奇怪的行爲與UserControl的多個實例。我正在嘗試設置ComboBoxItems的集合並將其綁定到UserControl的實例。但是,它將它們全部綁定到下面。有誰知道我會如何更改我的代碼,以便項目出現在預期的框中?WPF UserControl:collection綁定到所有實例?

非常感謝提前!

Items appear in all combos

窗口的Xaml

<Window x:Class="WpfUserControlSample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfUserControlSample" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel Orientation="Horizontal" Height="30"> 
     <local:ToolbarButtonCombo x:Name="one" Text="Combo 1..."> 
      <local:ToolbarButtonCombo.MenuItems> 
       <ComboBoxItem>Item 1</ComboBoxItem> 
       <ComboBoxItem>Item 2</ComboBoxItem> 
      </local:ToolbarButtonCombo.MenuItems> 
     </local:ToolbarButtonCombo> 
     <local:ToolbarButtonCombo x:Name="two" Text="Combo 2..."> 
      <local:ToolbarButtonCombo.MenuItems> 
       <ComboBoxItem>Item 3</ComboBoxItem> 
       <ComboBoxItem>Item 4</ComboBoxItem> 
      </local:ToolbarButtonCombo.MenuItems> 
     </local:ToolbarButtonCombo> 
     <local:ToolbarButtonCombo x:Name="three" Text="Combo 3..."></local:ToolbarButtonCombo> 
    </StackPanel> 
</Window> 

用戶控件的Xaml

<UserControl x:Class="WpfUserControlSample.ToolbarButtonCombo" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="30" 
      Name="Control"> 
     <StackPanel Orientation="Horizontal"> 
       <Button Name="btn" Command="{Binding Path=ButtonCommand,ElementName=Control}"> 
        <TextBlock Text="{Binding Path=Text, ElementName=Control, FallbackValue=Label}"></TextBlock> 
       </Button> 
       <ComboBox x:Name="cmb" Margin="0,0,0,0" 
          ItemsSource="{Binding Path=MenuItems,ElementName=Control}"> 

       </ComboBox> 
     </StackPanel> 
</UserControl> 

用戶控件代碼隱藏

namespace WpfUserControlSample 
{ 
    /// <summary> 
    /// Interaction logic for ToolbarButtonCombo.xaml 
    /// </summary> 
    public partial class ToolbarButtonCombo : UserControl 
    { 
     public ToolbarButtonCombo() 
     { 
      InitializeComponent(); 
     } 

     public static readonly DependencyProperty ButtonCommandProperty = 
      DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(ToolbarButtonCombo)); 
     public ICommand ButtonCommand 
     { 
      get { return (ICommand)GetValue(ButtonCommandProperty); } 
      set { SetValue(ButtonCommandProperty,value);} 
     } 

     public static readonly DependencyProperty MenuItemsProperty = 
      DependencyProperty.Register("MenuItems", typeof(ObservableCollection<ComboBoxItem>), 
      typeof(ToolbarButtonCombo), new FrameworkPropertyMetadata(new ObservableCollection<ComboBoxItem>())); 
     public ObservableCollection<ComboBoxItem> MenuItems 
     { 
      get { return (ObservableCollection<ComboBoxItem>)GetValue(MenuItemsProperty); } 
      set { SetValue(MenuItemsProperty, value); } 
     } 

     public static readonly DependencyProperty TextProperty = 
      DependencyProperty.Register("Text", typeof(string), typeof(ToolbarButtonCombo)); 
     public string Text 
     { 
      get { return (string) GetValue(TextProperty);} 
      set { SetValue(TextProperty, value); } 
     } 
    } 
} 

回答

1

您的MenuItems屬性是錯誤的。

WPF中的收集屬性應爲read-only依賴項屬性,並且需要爲每個類實例(在構造函數中)創建一個新集合。

您對控件的所有實例使用相同的ObservableCollection<ComboBoxItem>實例(默認值),因此它們都共享相同的項目。

+0

完美 - 謝謝! –