1

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

数据上下文:

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”已更新。

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

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

4

1 回答 1

0

已经有类似的问题了。1. Silverlight MVVM 绑定更新以不希望的顺序触发


  1. http://www.codeproject.com/Articles/42988/Silverlight-Behaviors-and-Triggers-Making-a-Trigge.aspx描述了让事情正常工作的好方法

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

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

        }
于 2011-04-08T13:34:28.990 回答