【发布时间】:2010-10-07 05:05:24
【问题描述】:
背景: 我正在创建一个自定义列表框,每个列表框项目上都有单选按钮,所以本质上它将是一个 RadioButtonList。该控件完全在代码中创建。截至目前,控件呈现和行为正确并支持 2 个方向(水平/垂直)。列表框使用一个 ItemTemplate,它是一个 StackPanel,带有一个 RadioButton 和一个 TextBlock。
到目前为止,我已经能够通过使用将其背景设置为透明的样式来防止在选择项目时更改项目的背景颜色。
我也想为前景色做同样的事情。
基本上ListBox的Selection模式是single的,当一个item被选中时,我只希望它被RadioButton反映出来。
我正在使用以下代码来设置 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);
我的模板的 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);
FactoryTextBox 然后附加到 FactoryStackPanel 并设置为 ListBox 的 ItemTemplate。
At the moment, I have the background color being set to Transparent when the item is selected.由于文本默认设置为白色,因此在选择项目时它会在视觉上消失。我正在寻找一种在选择文本块时在其前景上设置颜色的方法。现在它可以是黑色的,但最终它将引用更高级别的字体颜色。
【问题讨论】:
标签: c# wpf listboxitem