Tom Xue
Tom Xue

Reputation: 3355

How to copy/clone a UI control in UWP

In my UWP code, I create a Button like below:

            TextBlock tb1 = new TextBlock();
            tb1.FontSize = 20;
            tb1.Foreground = new SolidColorBrush(Windows.UI.Colors.White);
            tb1.HorizontalAlignment = HorizontalAlignment.Left;
            tb1.VerticalAlignment = VerticalAlignment.Top;
            tb1.Margin = new Thickness(9, 16, 0, 0);
            tb1.Text = dataModel.playList_orderNum[i].ToString();

Now I want to make a new Button but with only a little difference, e.g. the Foreground. Is there any easy way to make it?

            TextBlock tb11 = new TextBlock();
            tb11.FontSize = 20;
            // tb11.Foreground = new SolidColorBrush(Windows.UI.Colors.White);
            tb11.HorizontalAlignment = HorizontalAlignment.Left;
            tb11.VerticalAlignment = VerticalAlignment.Top;
            tb11.Margin = new Thickness(9, 16, 0, 0);
            tb11.Text = dataModel.playList_orderNum[i].ToString();

            tb11.Foreground = new SolidColorBrush(Windows.UI.Colors.Black);

Upvotes: 1

Views: 188

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

I want to make a new Button but with only a little difference, e.g. the Foreground.

You could pack the making process with method that with Color and string parameter like the following.

private TextBlock MakeTBK(Color color, string text)
{
    TextBlock tb11 = new TextBlock();
    tb11.FontSize = 20;
    tb11.HorizontalAlignment = HorizontalAlignment.Left;
    tb11.VerticalAlignment = VerticalAlignment.Top;
    tb11.Margin = new Thickness(9, 16, 0, 0);
    tb11.Text = text;
    tb11.Foreground = new SolidColorBrush(color);
    return tb11;
}

Upvotes: 1

Related Questions