1

我正在尝试创建一个用户控件,该控件将项目集合直接接受到 XAML 中(不绑定 DataContext),例如 ComboBox:

    <ComboBox>
        <ComboBoxItem Content="test"/>
        <ComboBoxItem Content="test2"/>
        <ComboBoxItem Content="test3"/>
    </ComboBox>

此外,在我的控件类中,我希望接收在 XAML 上的 UC 内输入的项目列表。这可能吗?

** 编辑 **

我需要收到多个列表,比如三个列表:

    <MyControl>
        <MyControl.FirstCollection>
            <MyControlItem Content="test"/>
            <MyControlItem Content="test2"/>
            <MyControlItem Content="test3"/>
        </MyControl.FirstCollection>
        <MyControl.SecondCollection>
            <MyControlItem Content="test4"/>
            <MyControlItem Content="test5"/>
            <MyControlItem Content="test6"/>
        </MyControl.SecondCollection>
        <MyControl.LastCollection>
            <MyControlItem Content="test7"/>
            <MyControlItem Content="test8"/>
            <MyControlItem Content="test9"/>
        </MyControl.LastCollection>
    </MyControl>

我怎样才能收到三个列表?

4

1 回答 1

1

我不想使用容器使我的项目可见,我只想获取 Children 控件并将其放入 List 或 Collection

如果它们不是视觉元素,您可以在用户控件上创建列表

示例:(仅使用FrameworkElement列表)

用户控制:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public ObservableCollection<FrameworkElement> FirstCollection
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(FirstCollectionProperty); }
        set { SetValue(FirstCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for FirstCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FirstCollectionProperty =
        DependencyProperty.Register("FirstCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>()));

    public ObservableCollection<FrameworkElement> SecondCollection
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(SecondCollectionProperty); }
        set { SetValue(SecondCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SecondCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SecondCollectionProperty =
        DependencyProperty.Register("SecondCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>()));

    public ObservableCollection<FrameworkElement> LastCollection
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(LastCollectionProperty); }
        set { SetValue(LastCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LastCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LastCollectionProperty =
        DependencyProperty.Register("LastCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>()));

}

用法:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication8"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid>
        <local:UserControl1 >
            <local:UserControl1.FirstCollection>
                <Label />
            </local:UserControl1.FirstCollection>
            <local:UserControl1.SecondCollection>
                <Label />
            </local:UserControl1.SecondCollection>
            <local:UserControl1.LastCollection>
                <Label />
            </local:UserControl1.LastCollection>
        </local:UserControl1>
    </Grid>
</Window>
于 2013-08-09T02:43:14.913 回答