WPF - 非常基本的ListBox.ItemTemplate问题

时间:2010-03-16 17:26:19

标签: wpf listbox datatemplate itemtemplate

好吧,这是一个令人尴尬的简单问题,但让我发疯。我正在学习DataTemplating,并且我正在尝试将非常简单的ItemTemplate应用于ListBox。

然而,当我运行我的应用程序时,模板被完全忽略,我只是得到标准的列表框,而实际上我希望看​​到一个带有'Test'的复选框列表。

我已经尝试过几次并且总是一样的结果。我在Google上查了几个资源,并且在ListBox上都有相同的语法定义和ItemTemplate,所以我真的看不出我出错的地方。

...代码

<Grid x:Name="LayoutRoot">
    <ListBox x:Name="TestList"
        SelectionMode="Multiple">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <CheckBox Content="Check this checkbox!"/>
                    <TextBlock>Test</TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.Items>
            <ListBoxItem>Bob</ListBoxItem>
            <ListBoxItem>Jim</ListBoxItem>
            <ListBoxItem>Dave</ListBoxItem>
            <ListBoxItem>Larry</ListBoxItem>
            <ListBoxItem>Tom</ListBoxItem>
        </ListBox.Items>            
    </ListBox>
</Grid>

任何帮助非常感谢。对不起这个看似愚蠢的问题,但我真的陷入了第一道障碍:(

AT

3 个答案:

答案 0 :(得分:18)

将ListBoxItem直接作为项放置时,

ItemTemplate不起作用。一般概念是您将CRL集合数据绑定到ListBox.ItemsSource,然后指定ItemTemplate。请检查以下代码。

 <Grid x:Name="LayoutRoot">
        <ListBox x:Name="TestList"  SelectionMode="Multiple">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <CheckBox Content="Check this checkbox!"/>
                        <TextBlock Text="{Binding}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.Items>
                <sys:String>Bob</sys:String>
                <sys:String>Jim</sys:String>
                <sys:String>Dave</sys:String>
                <sys:String>Larry</sys:String>
                <sys:String>Tom</sys:String>
            </ListBox.Items>
        </ListBox>
    </Grid>

其中sys是xmlns:sys =“clr-namespace:System; assembly = mscorlib”

通过这种方式,有5个ListBoxItems在后台生成并添加到ListBox中。

答案 1 :(得分:7)

如果要将ListBoxItems直接添加到ListBox,可以使用ItemContainerStyle而不是ItemTemplate。

但是,仅在每个项目级别需要独特特征时才建议这样做。

如果您计划使用ItemsSource查看相同或制作动态列表的所有项目,我建议您将字符串(或其他自定义对象)添加到列表中,并使用ItemTemplate显示您的项目。 (见Jobi Joy的回答)

以下是使用ItemContainerStyle的示例:

    <ListBox
        x:Name="TestList"
        SelectionMode="Multiple">

        <ListBox.ItemContainerStyle>
            <Style
                TargetType="ListBoxItem">

                <Setter
                    Property="Template">
                    <Setter.Value>
                        <ControlTemplate
                            TargetType="ListBoxItem">
                            <StackPanel>
                                <CheckBox
                                    Content="Check this checkbox!" />
                                <TextBlock
                                    Text="{TemplateBinding Content}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.Items>
            <ListBoxItem>Bob</ListBoxItem>
            <ListBoxItem>Jim</ListBoxItem>
            <ListBoxItem>Dave</ListBoxItem>
            <ListBoxItem>Larry</ListBoxItem>
            <ListBoxItem>Tom</ListBoxItem>
        </ListBox.Items>
    </ListBox>

答案 2 :(得分:0)

由于某些原因,如果使用ItemsSource填充ListBox,仍然可以忽略DataTemplate,例如:

    <ListBox Name="Test" x:FieldModifier="public" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

请注意,这绑定到包含具有一个属性的对象(TextAdapter:INotifyPropertyChanged)的ObservableCollection:string Text {...}