Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:50:57+00:00 2026-06-15T22:50:57+00:00

I have a button, and I have a boolean property in a view-model called

  • 0

I have a button, and I have a boolean property in a view-model called BooleanSwitch.

I want the text and forecolor of the button to be either:

  • green ON, when BooleanSwitch is true
  • red OFF, when BooleanSwitch is false

And I want it to change dynamically when the value changes.

Sure, I can do it in the codebehind.
But is there a XAML solution for this? I have tried the following:

<Style x:Key="SwitchButtonStyle" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=BooleanSwitch}" Value="true">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Content" Value="ON"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=BooleanSwitch}" Value="false">
            <Setter Property="Foreground" Value="#00AA00" />
            <Setter Property="Content" Value="OFF"/>
        </DataTrigger>
    </Style.Triggers>
</Style>


<Button Click="button_Click" Style="{DynamicResource SwitchButtonStyle}" />

And the click method is just:

private void button_Click(object sender, RoutedEventArgs e)
{
    BooleanSwitch = !BooleanSwitch;
}

The code of the BooleanSwitch couldn’t be simpler (.NET 4.5):

    private bool privateBooleanSwitch;
    public bool BooleanSwitch
    {
        get { return privateBooleanSwitch; }
        set
        {
            if (value == privateBooleanSwitch)
                return;

            privateBooleanSwitch= value;
            OnPropertyChanged();
        }
    }

When loaded, the triggers activate and the button is red with , but when I click the button, they no longer work…and I can see I’m probably not understanding well how DataTriggers work.

What am I doing wrong?

The button is a single purpose button but I’ve already found out I can’t use a DataTrigger inside a specific element. I was thinking about creating another properties containing text & color and then binding those to the button properties but that just seems to me as unneccessary code tailoring, I’d rather use XAML for defining colors and texts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:50:59+00:00Added an answer on June 15, 2026 at 10:50 pm

    Me.

    Dumb.

    Guess what.

    public abstract class ViewModelBase
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    I actually implemented the interface INotifyPropertyChanged but forgot to mention it in the class definition.

    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Instead of looking for a mistake here, I was certain there must be a mistake in my understanding of WPF triggers. Thanks to LPL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view , my view contains gallery and two button.Here i want
I have a button control that has its Content property bound to a boolean
I have a Login button that I want to be disabled until 3 text
i have a menu that contains just one item. Button exit; @Override public boolean
I have event Button that creates text box in run time. Private Sub Button1_Click(ByVal
I want to model a kind of FSM(Finite State Machine). I have a sequence
I am using the Model View View-Model pattern for a WPF app. I have
I have one textbox and one button. The Button Command should change the property
in my application i have list view with toggle button and textview.Here i am
I've a strange problem with binding a boolean property to a View's Visibility property.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.