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」は更新されていますが。

これはバグですか、それとも設計上の選択ですか? それは私にはバグのようです!それ以外の場合、イベントは Checking? と呼ばれるべきでした。:)

なぜこれが事実なのか、誰でも考えましたか?

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 に答える