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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2456643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:34:56  来源:igfitidea点击:

WPF - Very basic ListBox.ItemTemplate Question

wpflistboxdatatemplateitemtemplate

提问by Andy T

Ok, this is an embarassingly simple-looking problem, but is driving me crazy. I'm learning about DataTemplating and am trying to apply a very VERY simple ItemTemplate to a ListBox.

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

However, when I run my app, the template is completely ignored and I just get the standard-looking listbox, whereas in fact I'd expect to see a list of checkboxes with 'Test' along side.

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

I've tried this several times and always the same result. I've checked several resource on Google and all have the same kind of syntax for defining and ItemTemplate on a ListBox, so I really cannot see where I'm going wrong.

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

Code...

代码...

<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>

Any help greatly appreciated. Sorry for such a dumb-seeming question, but I've really fallen at the first hurdle here :(

非常感谢任何帮助。抱歉问了这么一个愚蠢的问题,但我真的在第一个障碍中掉了下来:(

AT

回答by Jobi Joy

ItemTemplatewont work when you put ListBoxItemdirectly as items. General concept is you databind a CRL collection to the ListBox.ItemsSourceand then specify the ItemTemplate. Check the below code.

ItemTemplate当您ListBoxItem直接作为项目放置时将无法使用。一般概念是将 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>

where sysis xmlns:sys="clr-namespace:System;assembly=mscorlib"

这里sysxmlns:sys="clr-namespace:System;assembly=mscorlib"

In this way, there are 5 ListBoxItemsgetting generated in the background and added to the ListBox.

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

回答by Kep Amun

You can use ItemContainerStyle instead of ItemTemplate if you want to add ListBoxItems directly to the ListBox.

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

Doing so, however, is only recommended when you need unique characteristics on a per item level.

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

If you are planning on all the items looking the same or making a dynamic list using ItemsSource, I would recommend you add strings (or another custom object) to your list and use ItemTemplate to display your items. (see Jobi Joy's answer)

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

Here's an example using ItemContainerStyle:

下面是一个使用 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>

回答by aliceraunsbaek

For some reason DataTemplate can still be ignored if the ListBox is populated using ItemsSource e.g:

出于某种原因,如果 ListBox 是使用 ItemsSource 填充的,则 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>

Note that this is bound to an ObservableCollection containing objects (TextAdapter : INotifyPropertyChanged) with one property: string Text {...}

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