change foreground color of every textblock element inside a gridHello, <div>I am starting to learn WPF and I've come across an issue I cant seem to be able to get around.</div> <div>I have a grid with some textBlocks in it. I want to change the color of the text in all of these depending on the value on a slider.</div> <div>In the &lt;grid&gt; tag I can set the property TextBlock.Foreground=&quot;#...&quot; and it sets the color for all its children that are TextBlocks.</div> <div><br/></div> <div>How can I set the TextBlock.Foreground property in a Grid element during runtime?</div> <div><br/></div> <div>many thanks.</div> <div><br/></div> <div>andraf.</div>© 2009 Microsoft Corporation. All rights reserved.Fri, 10 Jul 2009 09:29:07 Zae4cd800-3572-47ee-a850-abd55c83c41fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae4cd800-3572-47ee-a850-abd55c83c41f#ae4cd800-3572-47ee-a850-abd55c83c41fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae4cd800-3572-47ee-a850-abd55c83c41f#ae4cd800-3572-47ee-a850-abd55c83c41fandradfhttp://social.msdn.microsoft.com/Profile/en-US/?user=andradfchange foreground color of every textblock element inside a gridHello, <div>I am starting to learn WPF and I've come across an issue I cant seem to be able to get around.</div> <div>I have a grid with some textBlocks in it. I want to change the color of the text in all of these depending on the value on a slider.</div> <div>In the &lt;grid&gt; tag I can set the property TextBlock.Foreground=&quot;#...&quot; and it sets the color for all its children that are TextBlocks.</div> <div><br/></div> <div>How can I set the TextBlock.Foreground property in a Grid element during runtime?</div> <div><br/></div> <div>many thanks.</div> <div><br/></div> <div>andraf.</div>Thu, 02 Jul 2009 22:31:45 Z2009-07-02T22:31:45Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae4cd800-3572-47ee-a850-abd55c83c41f#a2d7a3e5-8b2e-4b85-a15c-5385ea991b97http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae4cd800-3572-47ee-a850-abd55c83c41f#a2d7a3e5-8b2e-4b85-a15c-5385ea991b97Rahul P Nathhttp://social.msdn.microsoft.com/Profile/en-US/?user=Rahul%20P%20Nathchange foreground color of every textblock element inside a gridHi,<br/> You can use <strong>DataTrigger</strong> .Based on the value in the cell you can assign different colors(or someother property).<br/> <br/> Here is an example.<br/> If you enter 5 in the textbox it will show in red<br/> <pre lang=x-xml>&lt;TextBox Height=&quot;23&quot; Margin=&quot;120,33,38,0&quot; Name=&quot;TextBox1&quot; VerticalAlignment=&quot;Top&quot; &gt; &lt;TextBox.Style&gt; &lt;Style TargetType=&quot;{x:Type TextBox}&quot;&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding=&quot;{Binding RelativeSource={RelativeSource Self},Path=Text}&quot; Value=&quot;5&quot;&gt; &lt;Setter Property=&quot;Foreground&quot; Value=&quot;Red&quot;/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TextBox.Style&gt; &lt;/TextBox&gt;</pre> <br/> You could also use a <strong>Converter</strong> on your foreground property and assign the color based on your requirement<br/> See this <a href="http://stackoverflow.com/questions/94177/wpf-datatrigger-not-firing-when-expected">link</a> for as sample in using converters(it might not be directly addressing your problem though)<br/> <br/> Hope it helps <hr class=sig> FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your queryFri, 03 Jul 2009 04:34:15 Z2009-07-03T04:40:05Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae4cd800-3572-47ee-a850-abd55c83c41f#134d10c8-ddc9-4c94-903f-ecab0dc1b6dahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae4cd800-3572-47ee-a850-abd55c83c41f#134d10c8-ddc9-4c94-903f-ecab0dc1b6daBruce.Zhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=Bruce.Zhouchange foreground color of every textblock element inside a gridHi andradf,<br/> <br/> Rahul has given useful suggestions and code. Here I wrote the code directly addressing your problems. The R property of the Color will change according to the value of the slider control. They are connected using databinding.<br/> <br/> <strong>XAML code:</strong> <br/> <br/> <pre lang=x-xml>&lt;Window x:Class=&quot;ChangeTextBlockForegroundProperty.Window1&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; xmlns:local=&quot;clr-namespace:ChangeTextBlockForegroundProperty&quot; Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt; &lt;Window.Resources&gt; &lt;/Window.Resources&gt; &lt;Grid Name=&quot;grid1&quot;&gt; &lt;Grid.Resources&gt; &lt;Style TargetType=&quot;TextBlock&quot;&gt; &lt;Style.Setters&gt; &lt;Setter Property=&quot;Foreground&quot;&gt; &lt;Setter.Value&gt; &lt;Binding ElementName=&quot;slider1&quot; Path=&quot;Value&quot;&gt; &lt;Binding.Converter&gt; &lt;local:MyConverter&gt;&lt;/local:MyConverter&gt; &lt;/Binding.Converter&gt; &lt;/Binding&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style.Setters&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;StackPanel&gt; &lt;TextBlock&gt;dddd&lt;/TextBlock&gt; &lt;TextBlock&gt;abc&lt;/TextBlock&gt; &lt;TextBlock&gt;ddd&lt;/TextBlock&gt; &lt;TextBlock&gt;eee&lt;/TextBlock&gt; &lt;Slider Name=&quot;slider1&quot; Maximum=&quot;255&quot; Minimum=&quot;0&quot;&gt;&lt;/Slider&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </pre> <strong>C# code:</strong> <br/> <br/> Implement the converter:<br/> <pre lang="x-c#"> public partial class Window1 : Window { public Window1() { InitializeComponent(); } } public class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { byte a = 0; Color c = Color.FromArgb(0,0,0,0) ; SolidColorBrush brush =null; if (value != null) { double tmp = double.Parse(value.ToString()); a = (byte)tmp; // Red color will change according to the value of the Slider c = Color.FromArgb(255, a, 0, 0); brush = new SolidColorBrush(c); } else { throw new ArgumentException(&quot;value can not be null&quot;); } return brush; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { //no need to implement here. throw new NotImplementedException(); } }</pre> <br/> After you change the value of the slider bar, you will see the Foreground color of the TextBlock changes.<br/> <br/> <br/> Best regards,<br/> Bruce Zhou<br/> <br/><hr class="sig">Please mark the replies as answers if they help and unmark if they don't.Thu, 09 Jul 2009 07:35:18 Z2009-07-09T07:35:18Z