Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to FontWeight in Silverlight 4 using a value converter

I want to compare two versions of various properties and bold one of them if it is not equal to the other. Since SL4 doesn't support MultiBinding I am binding the FontWeight to "." so that the entire data context is passed to the converter. I then use the converter parameter to specify which fields to compare within the converter. So far, so good... Values that don't match are bolded.

The problem is that the bolded property is bound to a text box which can be edited. When the value is edited, I want the converter to be "re-activated" so that the font weight is set according to the new value. This doesn't happen. How can this be accomplished?

Note: I have already implemented INotifyPropertyChanged for the relevant class and properties. Tabbing to the next field after changing the value causes the PropertyChanged event to fire, but the font weight is not updated until I actually move to a different record and then return to the record that was changed.

(I also tried using Mode=TwoWay to see if that would do the trick. However, TwoWay binding cannot be used when you are binding to ".")

like image 387
MylesRip Avatar asked Nov 06 '22 11:11

MylesRip


1 Answers

Do you NEED to use a Value Converter? I tried this quick using the MVVM pattern and it worked pretty well. If you could use MVVM, you could possibly do it like this:

MainPage.xaml

<UserControl x:Class="BindBoldText.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindBoldText"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.DataContext>
    <local:MainPage_ViewModel/>
</UserControl.DataContext>

<StackPanel>
    <TextBlock Text="{Binding Value1, Mode=TwoWay}"/>
    <TextBlock Text="{Binding Value2, Mode=TwoWay}" FontWeight="{Binding Value2FontWeight}"/>
    <TextBox Text="{Binding Value2, Mode=TwoWay}" TextChanged="TextBox_TextChanged"/>
</StackPanel>

MainPage.xaml.cs

    public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        this.viewModel = this.DataContext as MainPage_ViewModel;
    }

    private MainPage_ViewModel viewModel;

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {            
        viewModel.Value2 = (sender as TextBox).Text;
    }
}

MainPage_ViewModel.cs

public class MainPage_ViewModel : INotifyPropertyChanged
{
    public string Value1
    {
        get { return value1; }
        set
        {
            if (value1 != value)
            {
                value1 = value;
                OnPropertyChanged("Value1");
            }
        }
    }
    private string value1 = "Test";

    public string Value2
    {
        get { return value2; }
        set
        {
            if (value2 != value)
            {
                value2 = value;
                OnPropertyChanged("Value2");
                OnPropertyChanged("Value2FontWeight");
            }
        }
    }
    private string value2 = "Test";

    public FontWeight Value2FontWeight
    {
        get
        {
            if (value2.Equals(value1))
            {
                return FontWeights.Normal;
            }
            else
            {
                return FontWeights.Bold;
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}
like image 73
JSprang Avatar answered Nov 11 '22 20:11

JSprang