19

わかりました、これは恥ずかしいほど単純に見える問題ですが、私を夢中にさせています。私はDataTemplatingについて学び、非常に単純なItemTemplateをListBoxに適用しようとしています。

ただし、アプリを実行すると、テンプレートは完全に無視され、標準のリストボックスが表示されますが、実際には、[テスト]が横に付いたチェックボックスのリストが表示されると思います。

私はこれを数回試しましたが、常に同じ結果になりました。Googleでいくつかのリソースを確認しましたが、リストボックスで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>

どんな助けでも大歓迎です。そのようなばかげた質問で申し訳ありませんが、私はここで最初のハードルに本当に落ちました:(

4

3 に答える 3

26

ItemTemplateListBoxItem直接アイテムとして入れると動作しません。一般的な概念は、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ます。

于 2010-03-16T17:34:10.703 に答える
8

ListBoxItemsをListBoxに直接追加する場合は、ItemTemplateの代わりにItemContainerStyleを使用できます。

ただし、そうすることは、アイテムごとのレベルで固有の特性が必要な場合にのみ推奨されます。

同じように見えるすべてのアイテムを計画している場合、またはItemsSourceを使用して動的リストを作成する場合は、リストに文字列(または別のカスタムオブジェクト)を追加し、ItemTemplateを使用してアイテムを表示することをお勧めします。(ジョビジョイの答えを参照)

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>
于 2010-03-16T18:26:11.187 に答える
0

何らかの理由で、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>

これは、次の1つのプロパティを持つオブジェクト(TextAdapter:INotifyPropertyChanged)を含むObservableCollectionにバインドされていることに注意してください:string Text {...}

于 2013-08-29T09:13:11.457 に答える