Linking to FontWeight in Silverlight 4 Using a Value Converter

I want to compare two versions of different properties and one of them in bold if it is not equal to the other. Since SL4 does not support MultiBinding, I bind FontWeight to ".". so that the entire data context is passed to the converter. Then I use the converter parameter to indicate which fields to compare in the converter. So far so good ... Values ​​that do not match are shown in bold.

The problem is that the bold property is associated with a text field that 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 is not happening. How can I do that?

Note. I have already implemented INotifyPropertyChanged for the corresponding class and properties. When you click on the next field after changing the value, the PropertyChanged event occurs, but the font weight is not updated until I move to another record, and then return to the record that was changed.

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

+5
source share
2 answers

NEED to use a value converter? I tried this fast using the MVVM pattern and it worked very well. If you can use MVVM, you can 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
}
+2
source

, ".". . INotifyPropertyChanged , , , , , ​​- ​​ , .

, JSprang , , , . , ViewModel "ValuesAreSame", ( , ).

0

All Articles