1
votes

im working with the Silverlight CheckBox and RadioButton, and have the following scenario:

DataContext:

public class ViewModel : INotifyPropertyChanged
{
    bool isChecked;

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;
            InvokePropertyChanged("IsChecked");
        }
    }

    public void OnCheckBoxChecked()
    {
        // This function is run when the CheckBox Checked event triggers
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

View:

<UserControl x:Class="KSLog.Frontend.Features.CaseOverview.Views.CheckBoxView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="LayoutRoot" Background="White">
        <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Checked="HandleInViewModel"/>
    </Grid>
</UserControl>

When you click the checkbox, 'ViewModel.OnCheckBoxChecked' is run before the 'ViewModel.IsChecked' is updated via the binding. Although the 'CheckBox.IsChecked' has been updated.

Is this a bug or a designchoice? It seems as a bug to me! Otherwise the event should have been called Checking? :)

Any one got any thought on why this is the case?

1

1 Answers

0
votes

There been already similar question. 1. Silverlight MVVM binding updates fire in undesired order


  1. Also nice approach to get things to work described http://www.codeproject.com/Articles/42988/Silverlight-Behaviors-and-Triggers-Making-a-Trigge.aspx

Or my solution (c) ;)

        var element = FocusManager.GetFocusedElement() as TextBox;
        if (element!=null)
        {
            var binding = element.GetBindingExpression(TextBox.TextProperty);
            if (binding!=null)
            {
                binding.UpdateSource();
            }

        }