6 Replies - 6457 Views - Last Post: 20 January 2010 - 05:13 PM Rate Topic: -----

#1 boosh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-January 10

How to code a button that changes the color of the text in a wpf

Post icon  Posted 19 January 2010 - 10:54 PM

Hello all I need to know how to as you can guess from the topic how I can code a button for changing the text color of a Visual C# wpf program.
can anybody help?? :D
Is This A Good Question/Topic? 0
  • +

Replies To: How to code a button that changes the color of the text in a wpf

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: How to code a button that changes the color of the text in a wpf

Posted 20 January 2010 - 05:12 AM

This is a WPF question, so it should be in the WPF forum - I expect it will get moved there by an adim at soon!

I'm not sure exactly what you mean - do you mean a WPF button that when clicked will change the colour of all the text in the app, or just in a specific place?
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: How to code a button that changes the color of the text in a wpf

Posted 20 January 2010 - 05:41 AM

private void btnChangeColor_Click(object sender, RoutedEventArgs e)
{
	 lblSomeText.Foreground = Brushes.Green;
}



if you need something else, please be more specific
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: How to code a button that changes the color of the text in a wpf

Posted 20 January 2010 - 06:38 AM

Moved to WPF
Was This Post Helpful? 0
  • +
  • -

#5 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: How to code a button that changes the color of the text in a wpf

Posted 20 January 2010 - 07:11 AM

A very nice solution is to create a WPF Style for you foreground property.

For example, if you have 20 labels, instead of putting foreground = Brushes.Green, you can type in the style.

Then if you want to change things, it's simply a matter of modifying the style via code and everything else will change in cascade. :)

Read all about it here.
http://www.microsoft...ndtriggers.mspx
Was This Post Helpful? 0
  • +
  • -

#6 StCroixSkipper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 121
  • Joined: 23-December 08

Re: How to code a button that changes the color of the text in a wpf

Posted 20 January 2010 - 12:02 PM

View Postboosh, on 19 Jan, 2010 - 09:54 PM, said:

Hello all I need to know how to as you can guess from the topic how I can code a button for changing the text color of a Visual C# wpf program.
can anybody help?? :D


Here is the simple XAML, there is no actual code required...

<Window x:Class="ChangeButtonColor.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="Window1" Height="300" Width="300">
	<StackPanel>
		<StackPanel.Resources>
			<Style TargetType="{x:Type Button}">
				<Style.Triggers>
					<Trigger Property="Button.IsPressed" Value="True">
						<Setter Property="Button.Foreground" Value="Red"/>
					</Trigger>
				</Style.Triggers>
			</Style>
		</StackPanel.Resources>
		<Button>
			This is a button
		</Button>
	</StackPanel>
</Window>


Was This Post Helpful? 1
  • +
  • -

#7 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: How to code a button that changes the color of the text in a wpf

Posted 20 January 2010 - 05:13 PM

Just to put things in layman terms. The code posted by StCroix does the following:

It declares something to happen whenever a button is pressed.
That means, every single button you declare in that Stackpanel will adhere to this rule.

That something that happens is: setting the foreground to "Red".


Notice how he's just declaring a button without any other XAML declarations. That's because it's not needed anymore (at least in the Foreground regards).

All the best.

This post has been edited by papuccino1: 20 January 2010 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1