1

DataTemplate を含む ListBox があります。リストボックスで選択したアイテムのテキスト/前景の色を白に変更しようとしています。Googleで見つけた30の異なる方法を真剣に試しました。私はそれを機能させることができません。前景色を変更するにはどうすればよいですか?

また、SystemColors を使用するメソッドに依存したくないことを指摘したいと思います。.net 4.5 では機能しないことを読んだため、アプリケーションを4.5いつか。ここに私のリストボックスのxamlがあります:

<ListBox Grid.Row="1" x:Name="Alerts" ItemsSource="{Binding Alerts}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AlternationCount="2">
<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <Label Content="{Binding Type}" HorizontalAlignment="Left" Padding="1,1,1,0" />
                    <Label Content=" - " Padding="1,1,1,0"></Label>
                    <Label Content="{Binding Date}" HorizontalAlignment="Left" Padding="1,1,1,0" />
                </StackPanel>
                <Label Grid.Row="1" Content="{Binding Message}" HorizontalAlignment="Left" Margin="0" Padding="1,0,1,1" />
            </Grid>
            <Button Grid.Column="1"
                    CommandParameter="{Binding .}"
                    Command="{Binding Path=DataContext.Commands.RemoveAlertCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
                    Content="X" HorizontalAlignment="Right" Width="Auto" Height="Auto" Foreground="#FFD40000" FontWeight="Bold" VerticalAlignment="Center" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
    <Style  TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="LightGray"></Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="Ivory"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>
</ListBox>
4

1 に答える 1

9

これはどうですか:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

TextBlocks を使用する DataTemplate では、Foregroundプロパティは単純に継承されます。

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

Label コントロールを持つ DataTemplate では、Foreground値の継承が機能しません (理由についてはこちらをご覧ください)。ただし、次のように ListBoxItem の Foreground プロパティにいつでもバインドできます。

<ListBox.ItemTemplate>
    <DataTemplate>
        <Label Content="{Binding}"
               Foreground="{Binding Foreground,
                            RelativeSource={RelativeSource Mode=FindAncestor,
                                            AncestorType=ListBoxItem}}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

または、単にラベルを TextBlock に置き換えることもできます。

于 2013-02-10T22:24:54.977 に答える