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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:55:50+00:00 2026-05-27T07:55:50+00:00

I am new to WPF technology. i am using MVVM architecture. I want to

  • 0

I am new to WPF technology. i am using MVVM architecture.
I want to change the background of textblock based on viewmodel’s attribute.
e.g If i am using ‘brush’ object, I want to bind it to background color of textblock.

<TextBlock Margin="0,1"
                    HorizontalAlignment="Center"
                    FontFamily="Arial"
                    FontSize="16"
                    Text="{Binding Line}"
                    TextWrapping="Wrap"
                    Background="{Binding brushobj}"/>

How to implement it?

  • 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-05-27T07:55:50+00:00Added an answer on May 27, 2026 at 7:55 am

    You could define it like this, inside your ViewModel.

    private Brush _brushobj;
    /// <summary>
    /// Gets or sets the BrushObject.
    /// </summary>
    public Brush BrushObj
    {
        get
        {
            return _brushobj;
        }
        set
        {
    
            // Set value
            _brushobj = value;
    
            // Notify UI that the value has changed.
            RaisePropertyChanged("BrushObj");
            // OnPropertyChanged("BrushObj"); // Use the appropriate function to notify the UI.
        }
    }
    

    Then simply set the value with something like this:

    BrushObj = (Brush)new BrushConverter().ConvertFromString("Green");
    

    Last you bind it to your View like you did in your question:

    Background="{Binding BrushObj}"
    

    Edit: I tried to make a project myself just to verify that this was indeed working and the following code worked fine. If it’s still not working it’s more likely to be something with your MVVM setup causing problem.

    MainWindowViewModel:

    namespace WpfApplication1
    {
            public class MainWindowViewModel : ObservableObject
            {
               private Brush _brushobj = (Brush)new BrushConverter().ConvertFromString("Red");
               public Brush BrushObj
               {
                    get
                    {
                         return _brushobj;
                    }
                    set
                    {
                         _brushobj = value; // Set value
                         RaisePropertyChanged("BrushObj"); // Notify UI that the value has changed.
                    }
               }
          }
    }
    

    MainWindow:

        <Window x:Class="WpfApplication1.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>
                <TextBlock Margin="0,1"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            FontFamily="Arial"
                            FontSize="16"
                            Text="Testing a lot of stuff here! important stuff!"
                            TextWrapping="Wrap"
                            Background="{Binding BrushObj}"/>
            </Grid>
        </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to WPF technology and I am using MVVM architecture. I want
I'm pretty new to WPF and using the MVVM design pattern. To help learn
I am developing a game with WPF technology. I want to add new Features
Fairly new to wpf, using the galasoft mvvm templates. I have two relaycommands that
I have problem with WPF - I'm quiet new in this smart technology. So
being new to WPF this is a complex problem for me. What i want
I recently built a new application using WPF, so that I can learn the
I'm very new to this WPF technology, I started with a sticky note application
In a new WPF project (VS2010) i 'm using Unity 2 for the first
I've decided to use ClickOnce technology to deploy my new WPF application. By and

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.