locked
textblock foreground problem RRS feed

  • Question

  • 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
     

     

    Wednesday, January 30, 2008 2:26 AM

Answers

  • 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

    Wednesday, January 30, 2008 6:40 AM
  • 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"

    Wednesday, January 30, 2008 7:35 AM

All replies

  • 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.  

    Wednesday, January 30, 2008 2:58 AM
  • 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);

     

    Wednesday, January 30, 2008 3:16 AM
  • thx for the reply..it is workig cool.
     

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

    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.

     

    Wednesday, January 30, 2008 5:24 AM
  • 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

    Wednesday, January 30, 2008 6:40 AM
  • Great job..

    BTW, what is "theType"?

    Wednesday, January 30, 2008 7:27 AM
  • 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"

    Wednesday, January 30, 2008 7:35 AM
  •  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.  )

    Wednesday, January 30, 2008 7:40 AM
  • 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...

    Wednesday, January 30, 2008 8:00 AM
  • 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

    Thursday, January 31, 2008 8:17 AM