WPF UserControl:集合绑定到所有实例?

时间:2011-10-19 17:10:33

标签: c# wpf

我在WPF中看到一个带有UserControl的多个实例的奇怪行为。我正在尝试设置ComboBoxItems的集合并将其绑定到UserControl的实例。但相反,它将所有这些都绑定到它下面。有谁知道我如何更改我的代码,以便项目出现在预期的框中?

非常感谢提前!

Items appear in all combos

Window 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>

UserControl 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>

UserControl Codebehind

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 个答案:

答案 0 :(得分:1)

您的MenuItems财产有误。

WPF中的集合属性应该是read-only依赖项属性,而需要为每个类实例创建一个新集合(在构造函数中)。

您对控件的所有实例使用相同的ObservableCollection<ComboBoxItem>实例(默认值),因此它们共享相同的项目。