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

  • SEARCH
  • Home
  • 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 8106911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:36:25+00:00 2026-06-06T00:36:25+00:00

I am beginning WPF, and I am having a bit of a hard time

  • 0

I am beginning WPF, and I am having a bit of a hard time implementing data binding.

Specifically, I have created a simple user control which holds a Label and a Button.
For this user control, I have created a ViewModel which holds just two properties, string “Text” and SimpleEnum “Status”.

The point of the control is to display a status of something, like “Connected” yes/no, etc. The background color of the button indicates the status.

My XAML looks something like this

<Control.DataContext>
    <vm:OnOffStatusViewModel />
</Control.DataContext>

<Label x:Name="label1" Height="Auto" HorizontalAlignment="Left" Content="{Binding Text}" Width="280" />
<Button Style="{StaticResource GlassButton}" Height="14" Width="14" Background="{Binding Status}" Grid.Column="1" />

with xmlns:vm="clr-namespace:Controls"

The code-behind has a property ViewModel exposing the view model, implementing INotifyPropertyChanged, and initializes as _viewModel = (OnOffStatusViewModel) DataContext;

Now, in my view that is using this control, I have managed to set the Text to something, as I in my implementing view code-behind have onOffStatus1.ViewModel.Text = ..., however, the status is set by enum, and is as such not really bindable to the background property of the button.

My questions related to this:

  1. Is the way I have done the control correct? If not, what is the proper way of implementing data binding in user controls?

  2. How can I have my enum status update the background property of the button using binding?

  • 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-06T00:36:27+00:00Added an answer on June 6, 2026 at 12:36 am

    I would take a slightly different approach than the other answers here, I like to put the code and logic into my view models directly, so here’s how I would do it:

    <Control.DataContext>
        <vm:OnOffStatusViewModel />
    </Control.DataContext>
    
    <Label x:Name="label1" Height="Auto" HorizontalAlignment="Left" Content="{Binding Text}" Width="280" />
    <Button Style="{StaticResource GlassButton}" Height="14" Width="14" Background="{Binding ButtonBg}" Grid.Column="1" />
    

    In the VM:

    public MyStatus Status
    {
       get { return _status; }
       set
       {
          _status = value;
          OnPropertyChanged("Status");
    
          ButtonBg = Colors.Red;
       }    
    }
    
    public Color ButtonBg 
    {
       get { ... }
       set { ... }
    }
    

    Since your button background is bound to a property on your view model, then you have the freedom to change that in reaction to whatever is going on in your view model without needing to move logic or code out to converters and templates.

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

Sidebar

Related Questions

I'm attempting to create a simple WPF UserControl beginning with the default constructor. public
WPF VS 2010 C# Have a window where the beginning of the code behind
I'm currently working on a highlightable richtextbox control in WPF. The user can type
Since beginning to use VB.NET some years ago I have become slowly familiar with
I am only beginning to learn MVVM and its use in WPF. I am
I'm still in the beginning stage of learning c#, so i have a question
I've seen in WPF where you can bind control values to properties of other
I have a WPF application where I have defined custom commands that use the
I am beginning to heavily invest in WPF and was wondering what those more
I'm using VS2008 EE SP1 + WPF + XAML. In the beginning I added

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.