WPF拥有带有ItemsSource属性的UserControl

时间:2013-02-26 07:59:35

标签: wpf xaml user-controls itemssource

我想用ItemsSource属性创建一个自己的Usercontrol。 我创建了一个UserControl“myContainer”,我创建了一个UserControl“myItem”。现在我想在myContainer控件中显示myItem控件。所以我在myContainer控件中声明了一个依赖Property ElementsSource。 但是,如果我启动一个testproject并将一个集合绑定到itemssource属性,则不会发生任何事情。是实现itemssource属性的正确方法吗?

Xaml:myContainer

<UserControl x:Class="Control.myContainer"
         ...
         x:Name="myUserControl">
<Grid>
    <DockPanel x:Name="myDockPanel">

    </DockPanel>
</Grid>

myContainer背后的代码

public partial class myContainer : UserControl, INotifyPropertyChanged
{
    public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(myItem), typeof(myContainer));

    public myContainer()
    {
        InitializeComponent();
        DataContext = this;
    }

    public ObservableCollection<myItem> ItemsSource
    {
        get
        {
            return (ObservableCollection<myItem>)GetValue(ItemsSourceProperty);
        }
        set
        {
            SetValue(ItemsSourceProperty, value);
            OnPropertyChanged("ItemsSource");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Xaml myItem

<UserControl x:Class="Control.myItem"
         ... 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Border BorderThickness="1" BorderBrush="Black" CornerRadius="2">
        <DockPanel>
            <StackPanel DockPanel.Dock="Top" Background="LightGray">
                <DockPanel Margin="2,2,2,2">
                    <Button x:Name="Button_Close" DockPanel.Dock="Right" Width="14" Height="14" Margin="5,0,0,0" VerticalAlignment="Center"></Button>
                    <Button x:Name="Button_Undock" DockPanel.Dock="Right" Width="14" Height="14" Margin="5,0,0,0" VerticalAlignment="Center"></Button>
                    <TextBlock DockPanel.Dock="Left" VerticalAlignment="Center" FontWeight="Bold" Text="{Binding Header,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
                </DockPanel>
            </StackPanel>
            <ContentPresenter DockPanel.Dock="Top" ContentSource="Content"></ContentPresenter>
        </DockPanel>
    </Border>
</Grid>

CodeBehind myItem

 public partial class myItem : UserControl, INotifyPropertyChanged
{
    public static DependencyProperty _Header =
            DependencyProperty.Register("Header", typeof(String), typeof(myItem), new UIPropertyMetadata("Item"));

    public myItem()
    {
        InitializeComponent();
        DataContext = this;
    }

    public String Header
    {
        get
        {
            return (String)GetValue(_Header);
        }
        set
        {
            SetValue(_Header, value);
            OnPropertyChanged("Header");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

看起来你的项目类型基本上是这样的:

public class DataItem
{
    public string Header { get; set; }
    public object Content { get; set; }
}

您可以通过带有DataTemplate的ListBox显示此类型的项目,这些项目由ItemTemplate属性设置:

<ListBox ItemsSource="{Binding DataItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="3" BorderBrush="Aqua">
                <StackPanel Margin="10">
                    <TextBlock Text="{Binding Header}"/>
                    <ContentPresenter Content="{Binding Content}"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您当然可以在DataTemplate中使用专门的UserControl:

<DataTemplate>
    <Border BorderThickness="3" BorderBrush="Aqua">
        <StackPanel Margin="10">
            <TextBlock Text="{Binding Header}"/>
            <local:MyItemControl Content="{Binding Content}"/>
        </StackPanel>
    </Border>
</DataTemplate>

ListBox使用的默认面板是VirtualizingStackPanel。如果您想使用DockPanel,可以通过设置ItemsPanel属性来实现:

<ListBox ItemsSource="{Binding DataItems}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <DockPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    ...
</ListBox>

编辑:您可以通过在ItemsContainerStyle中的样式中替换其ControlTemplate,而不是使用DataTemplate,也可以完全替换ListBoxItem的视觉外观:

<ListBox ItemsSource="{Binding DataItems}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border BorderThickness="3" BorderBrush="Aqua">
                            <StackPanel Margin="10">
                                <TextBlock Text="{Binding Header}"/>
                                <ContentPresenter Content="{Binding Content}"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    ...
</ListBox>