以编程方式更改ListBox SelectedItem文本的颜色

时间:2015-09-07 00:07:19

标签: c# wpf xaml

我有一个包含一些项目的列表框。我需要这样做,当触发MouseDoubleClick事件时,所选列表框项的文本会改变颜色。我正在使用C#WPF。我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

向ListBox事件MouseDoubleClick添加事件处理程序并添加以下代码:

private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    SolidColorBrush b = new SolidColorBrush();
    b.Color = Color.FromRgb(255, 0, 255);
    ((ListBoxItem)((ListBox)sender).SelectedItem).Foreground = b;
}

然后将RGB颜色代码放在括号中。

如果您不使用ListBoxItems,请使用此通用代码:

private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    SolidColorBrush b = new SolidColorBrush();
    b.Color = Color.FromRgb(255, 0, 255);
    Type t = ((ListBox)sender).SelectedItem.GetType();
    if(t == new ListBoxItem().GetType())
        ((ListBoxItem)((ListBox)sender).SelectedItem).Foreground = b;
}

答案 1 :(得分:0)

这可以帮助您从XAML本身开始工作。

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="HorizontalContentAlignment"
            Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="VerticalContentAlignment"
            Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="Padding"
            Value="2,0,0,0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected"
                             Value="true">
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected"
                                       Value="true" />
                            <Condition Property="Selector.IsSelectionActive"
                                       Value="false" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

将此信息放入您的Window资源中。