2,000 Things You Should Know About WPF
Everything a WPF Developer Needs to Know, in Bite-Sized Chunks
- About Me
- Dependency Properties
- Visual Studio
#362 – Changing the Text on a ToggleButton When a User Clicks On It
August 11, 2011
It’s reasonable to want to change the text on a ToggleButton when a user clicks on it, so it shows the current state. You can do this by using a property trigger, which fires when the IsChecked property of the ToggleButton changes.
<StackPanel HorizontalAlignment="Center" Margin="15"> <Label Content="Click to arm the electric fence that surrounds your cube:"/> <StackPanel> <ToggleButton HorizontalAlignment="Center" Margin="5" Padding="5" Width="80"> <ToggleButton.Style> <Style TargetType="x:Type ToggleButton"> <Setter Property="Content" Value="Disarmed"/> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Content" Value="ARMED"/> <Setter Property="Foreground" Value="Red"/> </Trigger> </Style.Triggers> </Style> </ToggleButton.Style> </ToggleButton> </StackPanel> </StackPanel>
Filed under Controls
Tagged with Controls , Property Trigger , ToggleButton , Trigger , WPF
Software developer in the Twin Cities area, passionate about software development and sailing.
2 Responses to #362 – Changing the Text on a ToggleButton When a User Clicks On It
- email@example.com says:September 3, 2018 at 3:20 am
is ist possible to extend the example to change background- and foreground-color on click too?
- Sean says:September 3, 2018 at 7:29 am
Sorry Marco, each blog post is meant to show one small thing. Changing the Foreground or Background is quite easy though–just specify the Foreground or Background property in the trigger.Reply
Leave a Reply Cancel reply
- #1,219 – Expanding All Nodes in a TreeView by Default
- #1,218 – Stretching Items in TreeView across Entire Control
- #1,217 – Using Multiple HierarchicalDataTemplates in a TreeView
- #1,216 – Creating a Custom ItemTemplate in a TreeView
- #1,215 – Binding a TreeView to a Hierarchical Data Source
- 2,000 Things You Should Know About C#
- Britannica Geek
- Sean on Twitter
- Seans Stuff
|« Jul||Sep »|
#351 – Binding a CheckBoxs IsChecked Property to a Boolean Variable
#210 – Specifying Colors in XAML As RGB Values
#220 – Using the Predefined Colors
#375 – Binding Something in a Tooltip to a Property on the Parent Control
#920 – TextBox Sizes to Fit Its Content
#436 – Using a Drop Shadow with a Border
#1,204 – Using a DataTrigger to Change Content in a ContentPresenter
#431 – Binding a Controls Width to Its Height
#1,012 – Using a Different Data Template for the Face of a ComboBox
- 3,846,063 hits
Blog at WordPress.com.
How can i change the content “color” of a button in wpf
<StackPanel Margin="2"> <Button Name="btn" Click="btn_Click" Content="Load Profile Image"> <Button.Background> <ImageBrush ImageSource="D:\Pictures\rectangles.jpg"></ImageBrush> </Button.Background> </Button> </StackPanel>
Image that i am pasting here is of is almost black, so I want to change the content color to white so that it should appear properly.
My apology, I hadn’t added color in title which is quoted in commas, sorry for my mistake. Now it is OK, I think now it is clear to the reader 😉
- 1I’m unsure what you are asking here. Your ImageBrush takes the ImageSource (without altering it) and displays it as the background of your button. If you want the image to be "brighter", then you need to change the brightness of your image.
– Mike Eason
Feb 17 ’15 at 8:27
- Could you show us this ‘rectangles.jpg’ image?
– Łukasz Rejman
Feb 18 ’15 at 13:35
- Thank you Mike Eason and Lujasz Rejman.
Feb 18 ’15 at 13:50
add a comment |
Assuming you are talking about the color of the Text you are displaying you want the
<Button Name="btn" Click="btn_Click" Content="Load Profile Image" Foreground="White">
- yes, this is the answer of my question. Thank you Steve 🙂
Feb 18 ’15 at 13:42
- be sure to accept the answer
– Steve Mitcham
Feb 18 ’15 at 13:43
add a comment |
I don’t think we understand what you want here coz when you provide an image as background it will become background.
if you want Image , text and background to a button try this
<Button Name="btn" Background="Red"> <Button.Content> <Grid> <Image Source="D:\Pictures\rectangles.jpg"></Image> <TextBlock Text="Load Profile Image" ></TextBlock> </Grid> </Button.Content> </Button>
add a comment |
Here is the answer of my question, below is the code I was demanding.
Actually I wanted to foreground (text) color change.
<StackPanel Margin="2,10,2,0"> <Button Name="btn" Click="btn_Click" MouseEnter="btn_MouseEnter" MouseLeave="btn_MouseLeave" Content="Load Profile Image"> <Button.Background> <ImageBrush ImageSource="D:\Pictures\rectangles.jpg"></ImageBrush> </Button.Background> <Button.Foreground> <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" > <GradientStop Color="YellowGreen" Offset="0.25" /> <GradientStop Color="WhiteSmoke" Offset="1.5" /> </LinearGradientBrush> </Button.Foreground> </Button> </StackPanel>
Thank you for those, who tried to answer my unclear question 😉
add a comment |
Not the answer you’re looking for? Browse other questions tagged wpf or ask your own question .
3 years, 9 months ago
3 years, 9 months ago
How to get controls in WPF to fill available space?
How do I use WPF bindings with RelativeSource?
How can I find WPF controls by name or type?
How to programmatically click a button in WPF?
How to hide close button in WPF window?
How to bind inverse boolean properties in WPF?
How do I get the color from a hexadecimal color code using .NET?
How to get a group of toggle buttons to act like radio buttons in WPF?
How do I exit a WPF application programmatically?
Custom WPF TabItem Style has Ghost line?
Hot Network Questions
Why the hood is also called bonnet?
What are these "pimples" on the jet engine for?
How would a composite variable be strongly correlated with one variable but not the other?
What type of crank fitting is this?
Why does an instruction include the address of the next instruction on the IBM 650?
Why are fuel tanks located in wings?
O(·) is not a function, so how can a function be equal to it?
Are there any gambits involving sacrificing a piece?
Are there mistakes on the pre-rolled Basic Set wizard and ranger character sheets?
Is Voyager 2 capable of proving the existence of Oort cloud?
9sums – a logical deduction puzzle
My cat is not eating or drinking anything after the death of the fellow cat
Unavailable manager says ‘I’m always available’
How much water should I let my dog drink?
Calling a rabbit a "smeerp"
Alternative to "queer the deal"?
Book about old man storing magical power into plates
Given the result of grep -n, how can I open vim in that specific line? (using only keyboard)
How to tell how sensitive a photodiode is?
What’s the best way to store over 400 GB of digital photos?
"Famous" Jewish painting "The Talmud" pedigree
How much dirt did Galadriel give Sam?
With Jeremy Crawford taking over Acquisitions, Inc, will his rulings on the show be considered RAW?
A function to fill in a column with NA of the same type
more hot questions