按钮单击WPF MVVM后,Combobox绑定中断

时间:2013-12-27 19:55:35

标签: c# .net wpf silverlight mvvm

按钮单击wpf mvvm后,

Combobox绑定中断。我使用交互触发器将组合框选择绑定到ICommand

 <ComboBox  IsEditable="True" Text="Please Select" IsReadOnly="True" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ComboBox>
 <Button Content="Save"  Command="{Binding SaveCommand}" />

绑定工作正常,直到触发保存按钮的命令。触发按钮的保存命令后,组合框selectionchange命令会中断

编辑: Items是一个字符串列表,如下面的

           "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", 
           "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", 
           "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", 
           "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8"

所选项目显示在只读文本框中,当字母更改时,SelectionChangedCommand绑定中断(SelectedItem不再更新,但在单击保存时,已更新)。

请注意,Combobox的SelectedItem绑定到ViewModel的以下属性。

 private string _SelectedItem;
            public string SelectedItem
            {
                get
                {
                    return _SelectedItem;
                }
                set
                {
                    if (_SelectedItem!= value)
                    {
                        _SelectedItem= value;
                        RaisePropertyChanged(() => SelectedItem);
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

我想知道为什么要通过“SelectedItem”和InvokeCommandAction获取两者。在SeletedItem的设置器中,您可以执行您想要的操作,并在手中保留新选择。