【问题标题】:Silverlight ToggleButton checked event triggered before binding is updated? Bug?在更新绑定之前触发 Silverlight ToggleButton 检查事件?漏洞?
【发布时间】:2011-06-03 22:15:05
【问题描述】:

我正在使用 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.OnCheckBoxChecked”会在“ViewModel.IsChecked”通过绑定更新之前运行。虽然 'CheckBox.IsChecked' 已更新。

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

有人知道为什么会这样吗?

【问题讨论】:

    标签: silverlight data-binding checkbox togglebutton


    【解决方案1】:

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


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

    【讨论】:

      猜你喜欢
      • 2017-12-10
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      相关资源
      最近更新 更多