1

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

2

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>
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks, I tried the same. But its still not working. Background color haven't changed yet.
Make sure that you have INotifyPropertyChanged implemented properly in your ViewModel if it isn't updating. You could also try to add UpdateSourceTrigger=PropertyChanged to your XAML code. Background="{Binding BrushObj, UpdateSourceTrigger=PropertyChanged}"
should I have to uncomment //OnPropertyChanged("BrushObj") event?
It depends on how you implement INotifyPropertyChanged. If the first code compiled without problems it should be fine, depends on what framework and such you use for MVVM; like for example PRISM or MVVM Light.
If you are having problems, feel free to post some additional code from your ViewModel and I'll take a look. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.