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

And*_*y T 15 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>
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢.抱歉这样一个看似愚蠢的问题,但我真的堕落在这里的第一个障碍:(

Job*_*Joy 22

将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>
Run Code Online (Sandbox Code Playgroud)

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

这样,有5个ListBoxItems在后台生成并添加到ListBox中.


Kep*_*mun 8

如果要将ListBoxItem直接添加到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>
Run Code Online (Sandbox Code Playgroud)