相关疑难解决方法(0)

设置背景颜色或WPF(4.0)ListBox - Windows 8

我试图将所选ListBoxItem的背景颜色设置为白色而不是系统颜色.我已经阅读了我在这里可以找到的内容并且已经跟随或者相信已经遵循了那里的建议(更改所选ListBox项目的背景颜色,WPF如何在列表框失去焦点时更改列表框所选项目文本颜色,更改选中和不专心的列表框样式不会变灰,以及其他).

所有似乎都通过将HighlightBrush和ControlBrush设置为所选项目的透明来解决问题.我有以下XAML并且它正确设置了字体颜色,但无论画笔设置如何,backgroound都是默认的透明蓝色.我仍然是一个WPF菜鸟,所以我必须在这里遗漏一些简单的东西.

<ListBox Width="Auto" Height="Auto" Grid.Column="0" BorderThickness="0" Background="#FFF3F3F3" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ListBox.ItemsSource>
       <x:Array Type="{x:Type sys:String}">
          <sys:String>String 1</sys:String>
          <sys:String>String 2</sys:String>
          <sys:String>String 3</sys:String>
          <sys:String>String 4</sys:String>
       </x:Array>
    </ListBox.ItemsSource>
    <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
          <Style.Resources>
             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
          </Style.Resources>
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          <Setter Property="FontSize" Value="16"/>
          <Setter Property="Foreground" Value="#999999"/>
          <Style.Triggers>
             <Trigger Property="IsSelected" Value="True" >
                <Setter Property="Background" Value="White" />
                <Setter Property="Foreground" Value="Black" />
             </Trigger>
          </Style.Triggers>
       </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
       <DataTemplate>
          <TextBlock Text="{Binding}" HorizontalAlignment="Right" Margin="0,0,8,0" …
Run Code Online (Sandbox Code Playgroud)

c# wpf windows-8

17
推荐指数
2
解决办法
8775
查看次数

WPF更改ListboxItem选中时突出显示颜色

我在设置WPF中HighlightBrushKey的a SelectedItem时遇到了问题Listbox.我的目的是根据给定的布尔值设置Item的颜色,位于代码中.

我试过以下步骤:

  • 实现转换器,检查布尔值并返回正确的颜色.

    例:

    <ribbon:RibbonWindow.Resources>
      <l:WindowControl x:Key="ListBoxItemBackgroundConverter" />
        <Style x:Key="listBoxStyle" TargetType="{x:Type ListBoxItem}">
          <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={x:Static SystemColors.HighlightBrushKey}, Converter={StaticResource ListBoxItemBackgroundConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{Binding Source={x:Static SystemColors.ControlBrushKey}, Converter={StaticResource ListBoxItemBackgroundConverter}}"/>
          </Style.Resources>
        </Style>
    </ribbon:RibbonWindow.Resources>
    
    Run Code Online (Sandbox Code Playgroud)

    这里的问题是Convert方法只被调用一次,但我每次选择一个项目并检查布尔值时都需要调用Converter.像触发器一样,但带有" HighlightBrushKey".

    转换器:

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
       if(currentField == null)
          return Brushes.Yellow;
       if (currentField.Save)
          return Brushes.LightGreen;
       else
          return Brushes.Yellow;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 我的下一个想法是将" HighlightBrushKey" 设置为" Transparent"并item.Background在代码中手动更改.这里的问题是我的物品变白了,无法看到背景颜色

    例:

    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" …
    Run Code Online (Sandbox Code Playgroud)

wpf listboxitem background-color

8
推荐指数
1
解决办法
7049
查看次数

为ListBox中的选定项设置背景颜色

我无法在列表框中设置所选项目的背景颜色.我不想在这个例子中交替颜色.我把它们作为测试,他们工作.触发器IsSelected随着字体粗体变为粗体而前景变为红色而触发.将高亮颜色画笔设置为SteelBlue无法达到预期效果,因为当ListBox失去焦点时它会消失.当ListBox失去焦点并且是我想要的时候,红色和粗体确实成立.我想要为所选项目拍摄并保持背景颜色.现在,所选项目的背景为白色,并在ListBox失去焦点时保持不变.感谢您的帮助,我将测试任何建议的修复程序.

    <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" Visibility="Visible" BorderThickness="2" Margin="1" Padding="2,2,7,2"
             ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" HorizontalAlignment="Left" 
             PresentationTraceSources.TraceLevel="High" AlternationCount="2" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="LightGreen"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="LightPink"></Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="Background" Value="SteelBlue" />
                        <Setter Property="Foreground" Value="Red" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Background="Transparent" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Run Code Online (Sandbox Code Playgroud)

wpf xaml background listbox selecteditem

7
推荐指数
2
解决办法
2万
查看次数

使用C#更改WPF Listbox SelectedItem文本颜色和高亮/背景颜色

我试图在运行时更改突出显示的(选定的)颜色和wpf列表框的突出显示的文本颜色.我尝试过创建一个样式并应用如下:

    Style s = new Style(typeof(ListBox));
    s.Resources.Add(SystemColors.HighlightBrushKey, Setting.ListSelectedColor);
    s.Resources.Add(SystemColors.HighlightTextBrushKey, Setting.ListSelectedTextColor);
    lstGames.Style = s;
Run Code Online (Sandbox Code Playgroud)

但这似乎什么都不做.有没有办法实现这个目标?

编辑:

根据建议,我尝试使用DynamicResources来实现这一目标,但到目前为止,这还没有成功.我的代码:

DYNAMICRESOURCES

<UserControl.Resources>
    <Color x:Key="ListTextSelectedColor"/>
    <Color x:Key="ListSelectedColor"/>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

列表框

        <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" 
             Name="lstGames" Margin="20" Grid.Row="2" Grid.Column="2" 
             SelectionChanged="lstGames_SelectionChanged" Grid.RowSpan="2" Grid.ColumnSpan="2" 
             Background="{x:Null}" BorderBrush="{x:Null}" SelectionMode="Single"
             FontSize="18" FontFamily="OCR A Extended">
        <Style TargetType="ListBox">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource ListSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource ListSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/>
            </Style.Resources>
        </Style>
    </ListBox>
Run Code Online (Sandbox Code Playgroud)

在C#中应用资源

this.Resources["ListSelectedColor"] = SETING.ListSelectedColor.Color;
this.Resources["ListTextSelectedColor"] = SETTING.ListSelectedTextColor.Color;
Run Code Online (Sandbox Code Playgroud)

c# wpf listbox

5
推荐指数
2
解决办法
2万
查看次数