简体   繁体   中英

Change ComboBox text but not selected item

I've got a ComboBox that contains an item for each FilterViewModel that is defined in my application. Each FilterViewModel has a DisplayName property that contains the name of the filter. The currently selected filter instance is saved in the SelectedFilter property.

I want the user to be able to select an existing item from the dropdown list, but also change the selected filter's name in the text box. Entering another name must not change the item selection. There is always one filter selected, it can never be that no filter is selected.

Here's the XAML code:

<ComboBox
    IsEditable="True" IsTextSearchEnabled="False"
    ItemsSource="{Binding Filters}"
    SelectedItem="{Binding SelectedFilter}"
    DisplayMemberPath="DisplayName"/>

I can't find a TextChanged event for the ComboBox, and simply letting it as-is doesn't change the selected item's bound text property, DisplayName (I'd have expected that).

What can I do to get it work as described above?

The background is that this ComboBox is used in my application's settings dialog where the filters can be managed (created, duplicated, renamed, deleted) and their filter conditions can be edited. I'd like to save me the extra button and window or TextBox to rename a filter but offer that function directly in-place. You see the filter name, just click on it and type to change it. But I'd also like to use the ComboBox to have a drop-down selection menu to switch filters.

This should allow you to change DisplayName of SelectedFilter without changing SelectedFilter itself:

<ComboBox 
    ItemsSource="{Binding Filters}" 
    SelectedItem="{Binding SelectedFilter}" 
    DisplayMemberPath="DisplayName"
    Text="{Binding SelectedFilter.DisplayName, Mode=TwoWay}"
    IsEditable="True" 
    IsTextSearchEnabled="False"/>

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