0

以下のようにリソースファイルにスタイルを定義しました

   <Style x:Name="ListBoxStyle" TargetType="ListBox" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBox">                    
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name,Mode=TwoWay}" 
                               Margin="5" 
                               Foreground="Red">
                    </TextBlock>
                    <TextBlock Text="{Binding Age,Mode=TwoWay}" 
                               Margin="5">
                    </TextBlock>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>    
</Style>

ここのデータテンプレートに何を入れるか迷っています

<ListBox x:Name="MyList" ItemsSource="{Binding }">
    <ListBox.ItemTemplate>
        <DataTemplate>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

使ってみた

<ContentPresenter Style="{StaticResource ListBoxStyle}"></ContentPresenter> 

そしてさえ

<ContentControl Style="{StaticResource ListBoxStyle}"></ContentControl>`

しかし、このエラーが発生しました

プロパティ 'System.Windows.FrameworkElement.Style' への割り当てに失敗しました。

DataTemplateカスタム スタイルを提供したい場合、タグの間に何を入れればよいですか?

4

1 に答える 1

0

試す:

<ListBox x:Name="MyList" ItemsSource="{Binding }">
    <ListBox.ItemTemplate>
        <DataTemplate>
<StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name,Mode=TwoWay}" 
                               Margin="5" 
                               Foreground="Red">
                    </TextBlock>
                    <TextBlock Text="{Binding Age,Mode=TwoWay}" 
                               Margin="5">
                    </TextBlock>
                </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

これで問題が解決します。

スタイルを定義する場合は、ListBox の外観 (背景、前景など) を定義します。ここでデフォルトのスタイルを取得できます:http://msdn.microsoft.com/en-us/library/cc278062(v=vs.95).aspx

ItemTemplate (これは DataTemplate です) は、リストの単一要素のデータ表示がどのように見えるかを定義します (バインディングなどを使用します...)。

MouseOver、Focussed などの単一要素のスタイルを定義する場合は、ListBoxItems のスタイルを記述します。ItemContainerStyle を介してリスト ボックスに追加できます。

<ListBox ItemContainerStyle="{StaticResource YourResourceKey}"/>
于 2010-12-18T14:16:02.437 に答える