Home > front end >  How To Add Properties To New Object That You're Instantiating?
How To Add Properties To New Object That You're Instantiating?

Time:07-12

var par = new Paragraph(new Run(""));
par.BorderBrush = Brushes.Black;
ThicknessConverter converter = new ThicknessConverter();
par.BorderThickness = (Thickness)converter.ConvertFromString("0.02cm");

I'm trying to later change "par" to have different text in it, but I don't know how to do it without resetting the properties:

par = new Paragraph(new Run("new text"));

Is there either a way to combine the lines setting the border brush and thickness with the instantiation of par, or just to change the text in the run?

CodePudding user response:

What you would want to do is create, and assign a variable, as the Run(); object. Run has a text property that you can change.

var run = new Run("");
var par = new Paragraph(new Run(""));
par.BorderBrush = Brushes.Black;
ThicknessConverter converter = new ThicknessConverter();
par.BorderThickness = (Thickness)converter.ConvertFromString("0.02cm");
run.Text = string.Empty; //Your code here

Changing the Run object should change what the variable par has.

CodePudding user response:

Since it will be working on references why don't you just create a string:

string text = "";

use that instead and then you can change it programmatically or through a text box or something.

CodePudding user response:

I think it's something like

var par = new Paragraph(new Run("")){BorderBrush= Brushes.Black, etc.};
  •  Tags:  
  • c#
  • Related