5 Replies - 1238 Views - Last Post: 19 May 2016 - 10:43 AM Rate Topic: -----

#1 Randor   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Converting a string to ContentAlignment

Posted 19 May 2016 - 09:21 AM

Hello all,

ok, so I have a combo box that grabs the users choice for text alignment in a label, however; I cannot figure out how to set the labels TextAlign value with it, i tried this:

Label1.TextAlign = CB.Value.ToString();



and I get: "Cannot Implicitily convert type 'String' to 'System.Drawing.ContentAlignment'"

which i know is that it has to be a ContentAlignment reference to use, but I cannot find any info on how to convert it from a string to a ContentAlignment

Thank you for any help...

Is This A Good Question/Topic? 0
  • +

Replies To: Converting a string to ContentAlignment

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5105
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Converting a string to ContentAlignment

Posted 19 May 2016 - 09:50 AM

Well...it's an enumeration. So you could parse it, but...if I recall correctly, you can set a DisplayMember and ValueMember on your combo box. Meaning the Display Text can be "Bottom Right", but the actual value can be ContentAlignment.BottomRight. Then you wouldn't have to parse or convert, just cast the SelectedValue to a ContentAlignment.

Thought I'd include some parsing code as well, since Enum parsing isn't quite as simple as integer parsing:

string calStr = "BottomRight";
ContentAlignment cal = (ContentAlignment)Enum.Parse(typeof(ContentAlignment), calStr);



But I would very much suggest not parsing and using the ValueMember correctly.
Was This Post Helpful? 1
  • +
  • -

#3 SubPar   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 21-October 13

Re: Converting a string to ContentAlignment

Posted 19 May 2016 - 09:59 AM

You could also use your combobox selected string then implement IValueConverter to Convert to the ContentAlignment type then just specify the Converter in your Xaml binding like so.

<Label Name="label1" Content ="Testing" HorizontalContentAlignment="{Binding HorizontalAlign,Converter={StaticResource ContentAlignmentConverter} }" />

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5105
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Converting a string to ContentAlignment

Posted 19 May 2016 - 10:16 AM

TextAlign as a property of Label suggests that this is a Windows Forms project, not WPF/XAML.
Was This Post Helpful? 0
  • +
  • -

#5 Randor   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: Converting a string to ContentAlignment

Posted 19 May 2016 - 10:30 AM

View PostCurtis Rutland, on 19 May 2016 - 12:16 PM, said:

TextAlign as a property of Label suggests that this is a Windows Forms project, not WPF/XAML.

Correct.
I adjusted my value member and it worked.. thank you..
Was This Post Helpful? 0
  • +
  • -

#6 SubPar   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 21-October 13

Re: Converting a string to ContentAlignment

Posted 19 May 2016 - 10:43 AM

View PostCurtis Rutland, on 19 May 2016 - 10:16 AM, said:

TextAlign as a property of Label suggests that this is a Windows Forms project, not WPF/XAML.


Whoops, totally missed that detail. Just glanced at the question over break.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1