简体   繁体   中英

Border around text range

I am creating a chat application using WPF. Text messages are added to Rich textboxes created at runtime and these boxes are added to a Stack panel.

I need to have different colored borders for messages from different users.

How can I set border at runtime around these messages?

You can use Border() :) ie:

Border b = new Border()
{
    BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x80, 0x80)),
    BorderThickness = new Thickness(1, 0, 1, 2),
    CornerRadius = new CornerRadius(1, 1, 1, 5)
};

You could have your StackPanel as a placeholder and add to it at runtime. sp.Children.Add( b );

EDIT: A sample:

void Main()
{
    StackPanel sp = new StackPanel();
    for (int i = 0; i < 10; i++)
    {
        Border b = new Border()
        {
            BorderBrush = new SolidColorBrush(
            i % 2 == 0
            ? Color.FromArgb(0xFF, 0x80, 0x80, 0x80)
            : Color.FromArgb(0xFF, 0xFF, 0x0, 0x0)
            ),
            BorderThickness = new Thickness(1, 0, 1, 2),
            CornerRadius = new CornerRadius(1, 1, 1, 5),
            Child = new RichTextBox()
        };
        sp.Children.Add(b);
    }

    new Window { Content = sp }.ShowDialog();
    Dispatcher.CurrentDispatcher.InvokeShutdown();
}

You can use the border brush property, any control that has border has this property for example if you want to add border color to a button you write the following:

myButton.BorderBrush = new SolidColorBrush(Color.FromArgb(a: 255, r: 204, g: 204, b: 204));

I hope this solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM