[英]WPF ComboBox (with IsEditable) - Prevent selected item from being deleted when removed from the list of choices
我有一個IsEditable
設置為 true 的ComboBox 。 如果我輸入自定義選項然后重置選項列表(甚至刪除所有選項),顯示的文本不會改變。 這正是我想要的。 但是,如果我選擇或鍵入列表中的選項,然后從列表中刪除該選項,則所選文本將重置為 null 或空字符串。 在第二個實例中,我希望所選文本不會更改(基本上變成自定義條目)。
我嘗試使用SourceUpdated
並將Handled
設置為 true ,但無論我嘗試用新的 ObservableCollection 替換它還是調用Clear
,事件都不會觸發。
此外,手動管理該值是有問題的,因為延遲事件會在控件本身上觸發以響應更改。 如果我使用以下代碼,則控件會在所有內容執行完畢后響應更新的列表(並重置值)。
string holdValue = SelectedListString;
ListOptions.Clear();
SelectedListString = holdValue;
這是一個“很高興擁有”,所以我希望有一個簡單的解決方案,並且不能使用 3rd 方庫。 最壞的情況,我想我可以做一些 hacky 更改跟蹤,然后防止在依賴屬性SelectedListString
上調用 setter 時修改值,但我希望有更簡單的東西。
您可以像這樣將“SelectedListString”屬性與“ComboBox”的“Text”屬性綁定
<ComboBox IsEditable="True"
ItemsSource="{Binding ListOptions}"
SelectedItem="{Binding SelectedListString}"
Text="{Binding SelectedListString}"/>
當您清除列表時,您會像以前一樣手動保留該值
string holdValue = SelectedListString;
ListOptions.Clear();
SelectedListString = holdValue;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.