24

テキストの横に画像が表示されるリストボックス用に、次のスタイルを作成しました。

<Style x:Key="ImageListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid SnapsToDevicePixels="true">
                                <Border x:Name="Border">
                                    <Grid Height="40">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Image
                                            x:Name="DisplayImage"
                                            Source="{Binding Path=ThumbnailImage}"
                                            Height="30"
                                            Width="30"
                                            Grid.Column="0"/>

                                        <ContentPresenter
                                            x:Name="DisplayText"
                                            HorizontalAlignment="Stretch"
                                            VerticalAlignment="Center"
                                            Grid.Column="1"/>
                                        <!--<ContentPresenter.Resources>
                                                <Style TargetType="{x:Type TextBlock}">
                                                    <Setter Property="Foreground" Value="Black"/>
                                                </Style>
                                            </ContentPresenter.Resources>-->

                                        <!--Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath, Converter={StaticResource myDisplayMemberConverter}}"-->
                                        <!--<Label
                                            x:Name="Text"
                                            Content="{Binding Path=FullNameAndTitle}"
                                            Foreground="Black"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            VerticalContentAlignment="Center"
                                            HorizontalAlignment="Stretch"
                                            Grid.Column="1"
                                            Height="40"/>-->
                                    </Grid>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <!--<Setter Property="FontWeight" Value="Bold" TargetName="DisplayText"/>-->
                                    <!--<Setter Property="Style" Value="{StaticResource SelectedTextStyle}" TargetName="DisplayText"/>-->
                                    <Setter Property="Background" Value="DarkBlue" TargetName="Border"/>
                                    <Setter Property="Width" Value="40" TargetName="DisplayImage"/>
                                    <Setter Property="Height" Value="40" TargetName="DisplayImage"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Grid>
                    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <ScrollViewer Margin="1,1,1,1" Focusable="false" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <StackPanel IsItemsHost="true"/>
                            </ScrollViewer>
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ListBox 自体の DisplayMemberPath を使用して (テキストに関して) 表示されるものをフィルタリングしているため、contentpresenter を使用する必要があります。

ListBox で項目が選択されているときに、FontWeight を Bold に設定し、Foreground を White に設定するだけです。

誰もこのような問題に遭遇しましたか? 私はいくつかの関連する質問を見てきましたが、人々は TextBlock を使用して問題を回避することができましたが、残念ながらできません。

pplが提供できる情報は大歓迎です。

乾杯

4

4 に答える 4

51

別の方法もあります。ContentPresenterこの属性に追加できます

TextBlock.Foreground="YourColour"

この場合、そのプロパティに対してアニメーションを使用することもできます。

于 2011-11-23T19:20:26.057 に答える
23

大丈夫です。この質問に自分で答えることができました。フォアグラウンド/フォントウェイトの定義が含まれていないコンテンツプレゼンターのフォアグラウンド/フォントウェイトを変更しようとしていました。

<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White"/>

つまり、次のものを削除します。

TargetName="DisplayText"
于 2008-11-03T13:24:22.470 に答える
12

この関連する回答に基づいて、次のような同様の問題を解決できました。

<Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" />
于 2011-11-27T20:51:15.203 に答える