检测绑定期间是否调用了选择更改

时间:2015-12-10 07:49:43

标签: c# wpf binding selectionchanged

C#和wpf检测的最佳方法是,在初始绑定期间是否调用了组合框选择更改事件?我只想处理用户通过ui执行选择时更改的内容。

没有绑定部分的Xaml:

<ComboBox SelectionChanged="OnChanged" />

C#-Code:

private void OnChanged(object sender, SelectionChangedEventArgs e)
{
    // Here i want to know, wheter called by using ui or from binding
}

谢谢!

1 个答案:

答案 0 :(得分:0)

而不是使用事件使用绑定

<ComboBox SelectedItem="{Binding SelectedItem}" />

在viewmodel中

SomeType _selectedItem;
public SomeType SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged();
        // ... put your logic when user change selection here
    }
}
// use this to change selection from viewmodel
public SomeType SelectedItemSet
{
    set
    {
        _selectedItem = value;
        OnPropertyChanged();
        // ... put your logic when selected item is set by viewmodel here
    }
}