ComboBox selected item won't update name unless I select a different item

时间:2018-02-26 18:00:59

标签: c# wpf mvvm

I have a ComboBox with a list of names of objects. When I perform my rename command, the selected item's name changes in the list of items but will not show the updated name at the top unless I click to a different object then back. Here is a picture of the problem:

"Test System 1" has been renamed to "test" but is not being updated in the selection unless I click out and back.

Here is my ComboBox xaml:

<ComboBox Name="CSCB" IsEditable="True" IsReadOnly="True" Margin="8"
                      ItemsSource="{Binding Systems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                      DisplayMemberPath="Name"
                      SelectedItem="{Binding SelectedCoord, Mode=TwoWay}"
                      Text="Select a Coordinate System"
                      />

Let me know if I should include more code. Thank you :)

1 个答案:

答案 0 :(得分:0)

不幸的是,您必须重新实现这样做的自定义combobox,或者您只需在combobox事件上手动刷新TextChanged项,如下所示:

  private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        CSCB.Items.Refresh();
    }

假设你的xaml看起来像那样:

 <StackPanel>
    <ComboBox Name="CSCB" IsEditable="True" IsReadOnly="True" Margin="8" 
              ItemsSource="{Binding Systems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
              DisplayMemberPath="Name"
              SelectedItem="{Binding SelectedCoord, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
              Text="Select a Coordinate System"
    />
    <TextBox Text="{Binding SelectedCoord.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBoxBase_OnTextChanged"></TextBox>

</StackPanel>