ItemsPanelTemplate不显示元素

时间:2012-01-13 18:00:29

标签: wpf itemspaneltemplate

我只是试着理解ItemsPanelTemplate的概念。为此,我建立了一个小样本解决方案。

我有一个带有以下代码的UserControl“MyListView”。

MyListView.xaml:

<UserControl x:Class="WpfApplication2.MyListView"
         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="300" d:DesignWidth="300">
<UserControl.Resources>
    <Style  TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Border BorderBrush="Black" BorderThickness="1" Margin="0" Padding="0" Width="100" Background="Gray">
                            <TextBlock Text="Text" HorizontalAlignment="Center"  />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<ListView ItemsSource="{Binding Path=TreeItemChildren}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">

            </StackPanel>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
</UserControl>

在MyListView.cs中,我添加了一个DependencyProperty来将数据绑定到应该显示的数据:

public partial class MyListView : UserControl
{
    public MyListView()
    {
        this.TreeItemChildren = new ObservableCollection<string>();
        this.TreeItemChildren.Add("Text0");
        this.TreeItemChildren.Add("Text1");
        this.TreeItemChildren.Add("Text2");

        InitializeComponent();
    }

    public ObservableCollection<string> TreeItemChildren
    {
        get { return (ObservableCollection<string>)GetValue(TreeItemChildrenProperty); }
        set { SetValue(TreeItemChildrenProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TreeItemChildren.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TreeItemChildrenProperty =
        DependencyProperty.Register("TreeItemChildren", typeof(ObservableCollection<string>), typeof(MainWindow), new UIPropertyMetadata(null));
}

当我现在尝试在MainWindow中使用此UserControl时,没有数据显示。这是什么原因?

1 个答案:

答案 0 :(得分:0)

您在控件模板中没有bound任何ListBoxItem属性,因此没有显示Content

通常你会替换它:

<TextBlock Text="Text" HorizontalAlignment="Center"  />

使用:

<ContentPresenter HorizontalAlignment="Center"/>

(如果模板化控件为ContentControl,则ContentPresenter会自动绑定到Content相关属性

同样ListView.ItemsSource绑定到DataContext上的属性(未设置且不应设置,因为它会干扰控件实例上的绑定),将其更改为以某种方式定位UserControl(例如,使用ElementNameRelativeSource)。

ItemsSource绑定应该导致绑定错误,了解how to debug them

相关问题