在更新绑定之前触发Silverlight ToggleButton检查事件?错误?

时间:2011-01-05 10:16:39

标签: silverlight data-binding checkbox togglebutton

我正在使用Silverlight CheckBox和RadioButton,并有以下场景:

的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));
    }
}

查看:

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

当您单击该复选框时,在通过绑定更新“ViewModel.IsChecked”之前运行“ViewModel.OnCheckBoxChecked”。虽然'CheckBox.IsChecked'已经更新。

这是错误还是设计选择?这对我来说似乎是一个错误!否则该事件应该被称为检查? :)

有人想过为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

已经有类似的问题了。  1. Silverlight MVVM binding updates fire in undesired order


  1. 使用http://www.codeproject.com/Articles/42988/Silverlight-Behaviors-and-Triggers-Making-a-Trigge.aspx
  2. 描述事情的好方法

    或我的解决方案(c);)

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