简体   繁体   English

Mahapps SplitButton在删除另一个元素的同时修改了SelectedItem

[英]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. 我正在使用带有SplitButton的ObservableCollection,并且在删除另一个元素时检测到SelectedItem已修改。

Status -> Collection = [A, B, C] and Selected = B 状态 ->收集= [A,B,C],并且已选择= B

Action -> Remove(C) 动作 ->删除(C)

Result -> Collection = [A, B] and Selected = A 结果 ->集合= [A,B]且已选择= A

Delete code: 删除代码:

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

XAML code: XAML代码:

<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. 如果我使用Combobox而不是SplitButton,则不会出现此问题。

It seems to be a bug, see Fix SplitButton SelectedIndex and SelectedItem #1798 看来是个错误,请参阅Fix SplitButton SelectedIndex和SelectedItem#1798

As workaround you could try to override default value for SplitButton.SelectedIndexProperty . 解决方法是,您可以尝试覆盖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 . 另请参见不覆盖Dependency属性的默认值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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