wpf 如何使用代码隐藏创建 StackPanel -> Border -> Background

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18020779/
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-09-13 09:21:16  来源:igfitidea点击:

How to use code-behind to create StackPanel -> Border -> Background

c#wpfbackgroundstackpanel

提问by Eric after dark

I'm trying to set properties of a TreeViewItem-> StackPanelin c# like this question. It seems to make a lot of sense until I get to the part where I try to edit the Backgroundin my Border. Bordershave Backgroundobjects in them, but for the life of me I can't set a color or anything. It seems to be inconsistent because I can add Contentto a Labelby simply saying, Content = "Title".

我正在尝试像这个问题一样在 c# 中设置 a TreeViewItem-> 的属性。这似乎很有意义,直到我到达我尝试在我的. 里面有物体,但对于我的生活,我无法设置颜色或任何东西。这似乎不一致,因为我可以通过简单地说,添加到 a 。StackPanelBackgroundBorderBordersBackgroundContentLabelContent = "Title"

Anyway, this is my code:

无论如何,这是我的代码:

public static TreeViewItem childNode = new TreeViewItem() //Child Node 
{
     Header = new StackPanel
     {
         Orientation = Orientation.Horizontal,
         Children =
         {
             new Border {
                 Width = 12,
                 Height = 14,
                 Background = ? //How do I set the background?
             },
             new Label {
                 Content = "Child1"
             }
         }
     }
}; 

PS - I have the same problem when trying to add a BorderBrush

PS - 尝试添加时我遇到了同样的问题 BorderBrush

Thank you!

谢谢!

回答by Anatoliy Nikolaev

Backgroundproperty accepts an Brush. Therefore, the code can set the color as follows:

Background属性接受一个Brush. 因此,代码可以设置颜色如下:

MyLabel.Background = Brushes.Aquamarine;

Or this:

或这个:

SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
MyLabel.Background = myBrush;

To set any color, you can use BrushConverter:

要设置任何颜色,您可以使用BrushConverter

BrushConverter MyBrush = new BrushConverter();

MyLabel.Background = (Brush)MyBrush.ConvertFrom("#ABABAB");

Setting the property to the LinearGradientBrushin code:

将属性设置为LinearGradientBrushin 代码:

LinearGradientBrush myBrush = new LinearGradientBrush();

myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myBrush.GradientStops.Add(new GradientStop(Colors.Green, 0.5));
myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0));

MyLabel.Background = myBrush;

For you it would look like this:

对你来说,它看起来像这样:

private void Window_ContentRendered(object sender, EventArgs e)
{
    TreeViewItem childNode = new TreeViewItem()
    {
        Header = new StackPanel
        {
            Orientation = Orientation.Horizontal,

             Children =
             {
                 new Border
                 {
                     Width = 12,
                     Height = 14,
                     Background = Brushes.Yellow, // Set background here
                 },

                 new Label 
                 {
                     Content = "Child1", 
                     Background = Brushes.Pink, // Set background here
                 }
             }
        }
    };

    MyTreeView.Items.Add(childNode);
}