如何使用code-背后打造的StackPanel - >边框 - >背景 [英] How to use code-behind to create StackPanel -> Border -> Background

查看:565
本文介绍了如何使用code-背后打造的StackPanel - >边框 - >背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个树型视图属性 - > 的StackPanel 在C#中像<一个href=\"http://stackoverflow.com/questions/8203316/adding-content-to-a-treeviewitem-programmatically?lq=1\">this问题。这似乎使很多的感觉,直到我得到的部分,我尝试在编辑背景我的边框 。在其中国界背景的对象,但对我的生活,我不能设置彩色或任何东西。这似乎是不一致的,因为我可以添加内容标签通过简单的说,内容=标题

I'm trying to set properties of a TreeViewItem -> StackPanel in c# like this question. It seems to make a lot of sense until I get to the part where I try to edit the Background in my Border. Borders have Background objects 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 Content to a Label by simply saying, Content = "Title".

无论如何,这是我的code:

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 - 尝试添加​​时,我有同样的问题 BorderBrush

感谢您!

推荐答案

背景属性接受。因此,code可以设置颜色如下:

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

MyLabel.Background = Brushes.Aquamarine;

或者这样:

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

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

To set any color, you can use BrushConverter:

BrushConverter MyBrush = new BrushConverter();

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

在code设置该属性的一个LinearGradientBrush

Setting the property to the LinearGradientBrush in code:

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

这篇关于如何使用code-背后打造的StackPanel - &GT;边框 - &GT;背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆