简体   繁体   中英

Mahapps SplitButton modifies SelectedItem while removing another element

I am using an ObservableCollection with a SplitButton and I have detected that SelectedItem is modified while removing another element.

Status -> Collection = [A, B, C] and Selected = B

Action -> Remove(C)

Result -> Collection = [A, B] and Selected = A

Delete code:

Datacollection.ToList().Where(x => x.field > 5).ToList().ForEach(y => Datacollection.Remove(y));

XAML code:

<mah:SplitButton ItemsSource="{Binding Datacollection}" SelectedItem="{Binding Selected}"/>

I expect Selected not to be modified if the removed item is different to the Selected. If I use Combobox instead of SplitButton, I dont have this problem.

It seems to be a bug, see Fix SplitButton SelectedIndex and SelectedItem #1798

As workaround you could try to override default value for SplitButton.SelectedIndexProperty .

SplitButton.SelectedIndexProperty.OverrideMetadata(typeof(SplitButton), new FrameworkPropertyMetadata(-1));

Put it to the some static constructor, which will be called. See also Dependency property default value not being overriden .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM