代码之家  ›  专栏  ›  技术社区  ›  CodeLikeBeaker

WPF-列表框背景色-代码隐藏

  •  1
  • CodeLikeBeaker  · 技术社区  · 17 年前

    以下是使用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;
        }
    

    现在,如果我更改代码以尝试使用列表框本身,我得到的只是一个白色背景。以下是代码:

    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;
        }
    

    如果你需要任何澄清我的问题,请让我知道。

    提前谢谢。

    1 回复  |  直到 17 年前
        1
  •  2
  •   CodeLikeBeaker    17 年前

    我解决了自己的问题。这是由于我自己的错误。

    我在列表框属性中有以下内容:

    Background=“{x:Null}”

    嗯,问题解决了。上面的代码有效。您可以通过代码将列表框的背景设置为渐变,只要不设置background=null:)

    谢谢