How can i change the content &quot
Friday, March 15, 2019

How can i change the content &quot

2,000 Things You Should Know About WPF

Everything a WPF Developer Needs to Know, in Bite-Sized Chunks

  • Home
  • About Me
  • Index
Posts
Comments

  • Controls
  • Layout
  • Events
  • Basics
  • Blend
  • Graphics
  • XAML
  • Miscellaneous
  • Dependency Properties
  • Visual Studio

#361 – Creating a ToggleButton Whose Content Is an Image
#363 – Calendar Control Basics

#362 – Changing the Text on a ToggleButton When a User Clicks On It

August 11, 2011
2 Comments

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>


Advertisements

Filed under Controls
Tagged with Controls , Property Trigger , ToggleButton , Trigger , WPF

About Sean
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

  1. phacops@gmx.de says:
    September 3, 2018 at 3:20 am

    Hello Sean,
    is ist possible to extend the example to change background- and foreground-color on click too?

    Cheers Marco

    Reply

    • 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

Sean Sexton

Recent Posts

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

Blogroll

  • 2,000 Things You Should Know About C#
  • Britannica Geek
  • Sean on Twitter
  • Seans Stuff

Calendar

August 2011
SMTWTFS
« Jul   Sep »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31  

Top Posts

  • #351 – Binding a CheckBoxs IsChecked Property to a Boolean Variable

  • #210 – Specifying Colors in XAML As RGB Values

  • #220 – Using the Predefined Colors

  • Index

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

Tags

Application
Background
Basics
Binding
Blend
Border
Brush
Button
Calendar
Canvas
CheckBox
Color
Colors
ComboBox
Commands
ContentControl
Controls
Cursor
Data Binding
Dependency Properties
Dependency property
DockPanel
Drag-and-Drop
Events
Expander
FlowDocument
Focus
Fonts
FrameworkElement
Gradient
Graphics
Grid
GridSplitter
GroupBox
HorizontalAlignment
Image
InkCanvas
Input
ItemsControl
Keyboard
Keyboard Focus
KeyDown
KeyUp
Layout
LinearGradientBrush
ListBox
Localization
Manipulation
Margin
Measure
Miscellaneous
Mouse
Panel
Popup
Radial Gradient
Resources
Routed Events
ScrollViewer
Shape
Slider
StackPanel
TabControl
Text
TextBlock
TextBox
Tooltip
Touch Input
Transforms
UIElement
Visual Studio
Window
Windows
WPF
WrapPanel
XAML

Blog Stats

  • 3,846,063 hits
Advertisements

Blog at WordPress.com.

%d bloggers like this:

    Stack Overflow



    1. Log In
      Sign Up

    2. current community


      • Stack Overflow

        help
        chat

      • Meta Stack Overflow

      your communities

      Sign up or log in to customize your list.

      more stack exchange communities

      company blog

      • Tour

        Start here for a quick overview of the site

      • Help Center

        Detailed answers to any questions you might have

      • Meta

        Discuss the workings and policies of this site

      • About Us

        Learn more about Stack Overflow the company

      • Business

        Learn more about hiring developers or posting ads with us

    By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .

    How can i change the content “color” of a button in wpf

    Ask Question


    up vote
    4
    down vote

    favorite

    1
    <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 😉

    wpf

    share | improve this question

    edited Feb 18 ’15 at 13:31

    asked Feb 17 ’15 at 5:23

    WasiF

    1,8701929

    • 1

      I’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.
      –  WasiF
      Feb 18 ’15 at 13:50

    add a comment  | 

    3 Answers
    3

    active

    oldest

    votes


    up vote
    7
    down vote

    accepted

    Assuming you are talking about the color of the Text you are displaying you want the Foreground property.

    <Button Name="btn" Click="btn_Click" Content="Load Profile Image" Foreground="White">

    share | improve this answer

    answered Feb 18 ’15 at 13:35

    Steve Mitcham

    4,29812351

    • yes, this is the answer of my question. Thank you Steve 🙂
      –  WasiF
      Feb 18 ’15 at 13:42

    • be sure to accept the answer
      –  Steve Mitcham
      Feb 18 ’15 at 13:43

    add a comment  | 


    up vote
    0
    down vote

    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>

    share | improve this answer

    answered Feb 17 ’15 at 8:57

    Muds

    3,08741529

    add a comment  | 


    up vote
    0
    down vote

    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 😉

    share | improve this answer

    answered Feb 18 ’15 at 13:39

    WasiF

    1,8701929

    add a comment  | 

    Not the answer you’re looking for? Browse other questions tagged wpf or ask your own question .

    asked

    3 years, 9 months ago

    viewed

    7,234 times

    active

    3 years, 9 months ago

    Related

    271

    How to get controls in WPF to fill available space?

    533

    How do I use WPF bindings with RelativeSource?

    236

    How can I find WPF controls by name or type?

    126

    How to programmatically click a button in WPF?

    186

    How to hide close button in WPF window?

    318

    How to bind inverse boolean properties in WPF?

    523

    How do I get the color from a hexadecimal color code using .NET?

    110

    How to get a group of toggle buttons to act like radio buttons in WPF?

    391

    How do I exit a WPF application programmatically?

    0

    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


    question feed

    default

    Stack Overflow works best with JavaScript enabled