Skip to main content

Microsoft Silverlight

Answered Question textblock foreground problemRSS Feed

(1)

hansmukh999
hansmukh999

Member

Member

14 points

19 Posts

textblock foreground problem

hi all.. can any body help in findaing way that vil help me to change foregroung property dinamacly from code behind(C#)
i have following  text block in mt MyTest.xaml

<TextBlock Opacity="0.0" RenderTransformOrigin="0.5,0.5" Width="140" Height="43"

Canvas.Left="2.0" Canvas.Top="1" FontSize ="0" FontStyle="Normal" Foreground="Red"

x:Name="overlayed_text1">

and am using folling to get/set values of text block from code behind (MtTest.Xaml.cs)

 // Setting/Getting text
public string TextElementText{
get
{
  return textElement.Text;
}
set
{
  textElement.Text = value;
}
}
// Setting/Getting Font Size
public double TextElementFontSize
{
get
{
   return textElement.FontSize;
}
set
{
  textElement.FontSize = value;
}
}
// Setting/Getting  Opacity
public double TextElementOpacity
{
get
{
  return textElement.Opacity;
}
set
{
  textElement.Opacity = value;
}
}

but stuck here, dont know how to assign foreground value by this..
public string TextElementForeground
{
set
{
textElement.Foreground = value
}
get
{
return textElement.Foreground;
}
}

i will be very thankful of positive reply that let me fix this..thx all
 

 

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: textblock foreground problem

hansmukh999:
can any body help in findaing way that vil help me to change foregroung property dinamacly from code behind
 

Page.xaml 

<TextBlock x:Name="myTextBlock" Text="This is my test!" /> 

Page.xaml.cs  ( Page_Loaded event)

myTextBlock.Foreground = new SolidColorBrush (Colors.Red); 

 

but your code looks like you are trying to create your own control.  

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


hansmukh999
hansmukh999

Member

Member

14 points

19 Posts

Re: Re: textblock foreground problem

thx for the reply..it is workig cool..now the problem is that how come i convert string to color. i am geting color name in string format

string colorToApply=""Yellow"; 

TextElementForeground = new SolidColorBrush(Color.xxxx);

 

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Re: textblock foreground problem

hansmukh999:
thx for the reply..it is workig cool.
 

Your welcome. Please mark as answered on that post if it's working. Thanks. :)  

hansmukh999:
how come i convert string to color. i am geting color name in string format
 

Normally, we used to convert the string to known-color by using TypeDescriptor. Unfortunately, I'm not able to use it in Silverlight project.

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


bluearc
bluearc

Participant

Participant

1500 points

325 Posts

Answered Question

Re: Re: textblock foreground problem

Ttyr this..

this will help you a lot..

 

 

using System;

using System.Windows.Media;

using System.Reflection;

namespace Ravs

{

public class ColorReflector

{

public Color GetThisColor(string colorString)

{

Type colorType = (typeof(System.Windows.Media.Colors));if (theType.GetProperty(colorString) != null)

{

object o = theType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);

if (o != null)

{

return (Color)o;

}

}

return Colors.Black;

}

}

}

 Save above code in any .cs file

 

to use this in your code

use it like this

Ravs.ColorReflector cr = new Ravs.ColorReflector();

string colorToApply=""Yellow"; 

TextElementForeground = new SolidColorBrush(cr.GetThisColor(colorToApply));

 

IF this answer helps you the mark it answered

Ravi Kant Srivastava
(Application Developer)
Scope Technology
India/South Africa

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Re: textblock foreground problem

Great job..

BTW, what is "theType"?

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


bluearc
bluearc

Participant

Participant

1500 points

325 Posts

Answered Question

Re: Re: textblock foreground problem

Sorry, I just forgot to change the variable.. initially i used theType , then i renamed to colorType... did a mistake in hurry...

Now it is fine.. have a look to the code given below... 

using System;

using System.Windows.Media;

using System.Reflection;namespace Ravs

{

public class ColorReflector

{

public Color GetThisColor(string colorString)

{

Type colorType = (typeof(System.Windows.Media.Colors));if (theType.GetProperty(colorString) != null)

{

object o = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);if (o != null)

{

return (Color)o;

}

}

return Colors.Black;

}

}

}

 Save above code in any .cs file

 

to use this in your code

use it like this

Ravs.ColorReflector cr = new Ravs.ColorReflector();

string colorToApply=""Yellow"; 

TextElementForeground = new SolidColorBrush(cr.GetThisColor(colorToApply));

 

"If this answer helps you then mark it answered"

Ravi Kant Srivastava
(Application Developer)
Scope Technology
India/South Africa

mchlSync
mchlSync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Re: textblock foreground problem

 Great.. Thanks a lot. (I can't mark it as answered since I'm not thread starter. However, I'm appreciate your code. I've learnt something today. :) I was looking too much for TypeDescriptor and ColorConverter.  )

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


bluearc
bluearc

Participant

Participant

1500 points

325 Posts

Re: Re: textblock foreground problem

Thanks for the comments, well it it don't matter that much if it is not marked as answered...

as long as i can contribute my part to the community....

Well, normally we don't look at thr Reflection... but i belive it is the most powerfull.... Namespace...

Ravi Kant Srivastava
(Application Developer)
Scope Technology
India/South Africa

hansmukh999
hansmukh999

Member

Member

14 points

19 Posts

Re: Re: textblock foreground problem

thx both of you, it is working cool now, as new to silver light ist is nt possible for me to do this, but your experienced help me , cant help saying u guys thanx again..

i vill disturb u guys again in future..heheh.chill n god bless u

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities