0

2 つのデータ テンプレートを作成し、それをリソースとして使用して、リスト ボックスに適用しています。リスト ボックスには 1 つのデータ テンプレートしか適用できません。両方のデータ テンプレートのコードは次のとおりです。

  <Window.Resources>
        <DataTemplate x:Key="template1">
            <Canvas Height="40" Width="850">
                <Label Height="30" Width="170" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
                <TextBox Height="30" Width="120" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
                <Label Canvas.Left="420" Canvas.Top="5">$</Label>
            </Canvas>
        </DataTemplate>
        <DataTemplate x:Key="template2">
            <Canvas Height="40" Width="850">
                <Label Height="30" Width="200" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
                <TextBox Height="30" Width="200" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
                <Label Canvas.Left="420" Canvas.Top="5">$</Label>
            </Canvas>
        </DataTemplate>
            </Window.Resources>

そしてリストボックスのコード

<TabItem>
        <Canvas Height="700" Width="850">
            <ListBox x:Name="listBox" Height="700" Width="850" ItemTemplate="{StaticResource template1}">
            </ListBox>
        </Canvas>
    </TabItem>

両方のデータ テンプレートをリスト ボックスに適用するにはどうすればよいですか。現在、「template1」のみが適用されています。「template2」を適用するにはどうすればよいですか。また、将来多くのデータ テンプレートが存在する場合はどうすればよいですか??,ありがとうございます。

4

2 に答える 2

1

DataTemplateSelector と Datatrigger を使用して複数の DataTemplates を使用できますが、条件 (選択されたアイテム、選択されていないアイテム、インデックス、フォアグラウンドとフォントサイズの変更など) を説明していません。

表示目的/UI目的のみのテンプレートが必要なようで、以下のようにリストボックスアイテムごとにテンプレートを変更する必要があります

<Window.Resources>
    <ControlTemplate x:Key="template1">
        <Canvas Height="40" Width="850">
            <Label Height="30" Width="170" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
            <TextBox Height="30" Width="120" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
            <Label Canvas.Left="420" Canvas.Top="5">$</Label>
        </Canvas>
    </ControlTemplate>
    <ControlTemplate x:Key="template2">        
        <Canvas Height="40" Width="850">
            <Label Height="30" Width="200" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
            <TextBox Height="30" Width="200" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
            <Label Canvas.Left="420" Canvas.Top="5">$</Label>
        </Canvas>     
    </ControlTemplate>      
</Window.Resources>
<Grid>
    <TabControl>
        <TabItem>
            <Canvas Height="700" Width="850">
                <ListBox x:Name="listBox" Height="700" Width="850">
                    <ListBoxItem Template="{StaticResource template1}"></ListBoxItem>
                    <ListBoxItem Template="{StaticResource template2}"></ListBoxItem>
                </ListBox>
            </Canvas>
        </TabItem>
    </TabControl>
</Grid>
于 2014-06-16T09:43:57.977 に答える