C# 所选列表框项目前景色的 Wpf 样式资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/541866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:27:59  来源:igfitidea点击:

Wpf style resource for foreground color of selected listbox item

c#wpflistboxitem

提问by BrandonS

Background: I am creating a custom listbox that has radio buttons on each listbox item, so essentially it will be a RadioButtonList. The control is created entirely in code. As of right now the control renders and behaves correctly and supports 2 orientations (Horizontal/Vertical). The listbox uses an ItemTemplate that is a StackPanel with a RadioButton and a TextBlock.

背景:我正在创建一个自定义列表框,它在每个列表框项上都有单选按钮,因此本质上它将是一个 RadioButtonList。该控件完全在代码中创建。截至目前,控件呈现和行为正确,并支持 2 个方向(水平/垂直)。列表框使用一个 ItemTemplate,它是一个带有 RadioButton 和 TextBlock 的 StackPanel。

So far I have been able to prevent the background color of the item from changing when the item is selected by using a style that sets it's background to transparent.

到目前为止,我已经能够通过使用将其背景设置为透明的样式来防止在选择项目时更改项目的背景颜色。

I would like to also do the same for the foreground color.

我也想对前景色做同样的事情。

Basically, the Selection mode of the ListBox is single, and when an item is selected, I only want it to be reflected by the RadioButton.

基本上,ListBox 的选择模式是单一的,当一个项目被选中时,我只希望它被 RadioButton 反映。

I am using the following code to set the ItemContainerStyle:

我正在使用以下代码来设置 ItemContainerStyle:

System.Windows.Style style =  
    new System.Windows.Style(typeof(System.Windows.Controls.ListBoxItem));  

System.Windows.Media.SolidColorBrush brush =  
    new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Transparent);  

style.Resources.Add(System.Windows.SystemColors.HighlightBrushKey, brush);

The TextBlock of my template is created using a System.Windows.FactoryFrameworkElement like this:

我的模板的 TextBlock 是使用 System.Windows.FactoryFrameworkElement 创建的,如下所示:

System.Windows.FrameworkElementFactory factoryTextBlock =   
    new System.Windows.FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock));
factoryTextBlock.SetBinding(System.Windows.Controls.TextBlock.TextProperty, new System.Windows.Data.Binding("Description"));  
factoryStackPanel.AppendChild(factoryTextBlock);

The FactoryTextBox is then appended to the FactoryStackPanel and is set as the ItemTemplate of the ListBox.

然后将 FactoryTextBox 附加到 FactoryStackPanel 并设置为 ListBox 的 ItemTemplate。

At the moment, I have the background color being set to Transparent when the item is selected. Since the text gets set to white by default, it visually disappears when the item is selected. I am looking for a way to set a color on the foreground of the textblock when it's selected. For now it can be black, but eventually it will reference a font color at a higher level.

At the moment, I have the background color being set to Transparent when the item is selected. 由于文本默认设置为白色,因此在选择项目时它会在视觉上消失。我正在寻找一种在文本块被选中时在其前景上设置颜色的方法。现在它可以是黑色,但最终它会引用更高级别的字体颜色。

回答by Robert Macnee

Here's an example using XAML, I'll leave the translation to C# up to you:

这是一个使用 XAML 的示例,我将把 C# 的翻译留给您:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <x:Array x:Key="data" Type="{x:Type sys:String}">
            <sys:String>sphinx</sys:String>
            <sys:String>of</sys:String>
            <sys:String>black</sys:String>
            <sys:String>quartz</sys:String>
        </x:Array>
    </Grid.Resources>
    <ListBox ItemsSource="{StaticResource data}">
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="Pink"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>
</Grid>