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 8400009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:27:16+00:00 2026-06-09T21:27:16+00:00

Here is the working code i have: The text and background color property do

  • 0

Here is the working code i have: The text and background color property do change when I click the button (but for a micro second) and are then set back to the default text/color. Seems like RaisePropertyChanged is being triggered again and again. Can somebody help point what I am doing wrong?

MainWindow.xaml code

 <Window x:Class="BuiltIn_Custom_Commands_Eg.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel HorizontalAlignment="Center">
        <TextBlock Text="{Binding txtblck_text, StringFormat=Default: {0}}" Padding="10" FontStyle="Italic" Background="{Binding txtblck_color}"/>
        <Button Content="Change Color" Width="100" Height="30" Margin="20" Command="{Binding OkCommand}" />
    </StackPanel>
</Grid>

ViewModel Code:

class Example_ViewModel : ViewModelBase
{
    #region Properties
    private string _txtblck_text;
    private Brush _txtblck_color;
    public ICommand OkCommand {get; set;}

    public string txtblck_text
    {
        get { return _txtblck_text; }
        set 
        { 
            _txtblck_text = value;
            RaisePropertyChanged("txtblck_text");
        }
    }

    public Brush txtblck_color
    {
        get { return _txtblck_color; }
        set
        {
            _txtblck_color = value;
            RaisePropertyChanged("txtblck_color");
        }
    }
    #endregion

    #region Constructor
    public Example_ViewModel()
    {
        OkCommand = new myCommand(myOkExecute, myCanOkExecute);
    }
    #endregion

    private void myOkExecute(object parameter)
    {
        txtblck_color = Brushes.CadetBlue;
        //RaisePropertyChanged("txtblck_color");

        txtblck_text = "You Clicked me!!!";
        //RaisePropertyChanged("txtblck_text");
    }

    private bool myCanOkExecute(object parameter)
    {
        txtblck_color = Brushes.Yellow;
        txtblck_text = "You havent clicked me!!!";
        return true;
    }
}
  • 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-09T21:27:18+00:00Added an answer on June 9, 2026 at 9:27 pm

    The CanExecute method will and should be called whenever bindings change. Therefore changing a binding in the Execute method (color) will cause CanExecute to be called again.

    Instead, why dont you initialize the colors private member once in the constructor as follows.

    public Example_ViewModel()
    {
        OkCommand = new myCommand(myOkExecute, myCanOkExecute);
        _txtblck_color =  = Brushes.Yellow;
    }
    

    Note, the same is also true for the text property. Normally all property private member should be set up with defaults on initialize (constructor) as this avoids unnecessary calls to INotifyPropertyChanged.

    Also, in order to test how the code is behaving and to confirm this just set some breakpoints in the CanExecute method to see how the program flow is behaving.

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

Sidebar

Related Questions

i have implemented code like here but it's not working for iOS 5.0, it
Here's the code for a simple page I have that is not working: <head>
How can I change the color of the text on my UIButton. Here is
I have a <input> element that I want to change the background color on.
Here's what I'm working with Code: <a href=# onclick=window.onpopstate = function() { alert('pop'); };
I thought I'd put the working solution here for others interested in a code
Here's my code that is not working: print To: ; my $to=<>; chomp $to;
here is some code I've been working on, basically I need to set up
Here's the code I'm currently working with: http://jsfiddle.net/gEMCm/ I'm trying to make an image
Here's my code I'm trying to get working to no avail: CREATE DEFINER=`auser`@`localhost` PROCEDURE

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.