【发布时间】:2009-05-09 23:24:24
【问题描述】:
我正在尝试在代码中设置列表框的背景颜色。我可以让它与列表框项目一起使用,但不是列表框本身。
这是有效的代码(使用 ListBoxItem):
private void SetBackgroundGradient()
{
var styleListBox = new Style(typeof(ListBoxItem));
var myBrush = new LinearGradientBrush();
myBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 0.0));
myBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, 255, 255, 255), 1.0));
styleListBox.Setters.Add(new Setter
{
Property = BackgroundProperty,
Value = myBrush
});
lstTopics.ItemContainerStyle = styleListBox;
}
现在,如果我更改代码以尝试使用 ListBox 本身,我得到的只是白色背景。这是代码:
private void SetBackgroundGradient()
{
var styleListBox = new Style(typeof(ListBox));
var myBrush = new LinearGradientBrush();
myBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 0.0));
myBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, 255, 255, 255), 1.0));
styleListBox.Setters.Add(new Setter
{
Property = BackgroundProperty,
Value = myBrush
});
lstTopics.Style = styleListBox;
}
知道我做错了什么吗?
如果您需要对我的问题进行任何澄清,请告诉我。
提前致谢。
【问题讨论】:
-
您的示例代码对我来说效果很好。也许您可以在调用函数的位置添加位和 XAML?
-
哇,你能设置列表框的背景吗?有趣.. 让我只用列表框创建一个新项目,看看我在主项目中是否做错了什么