我使用ComboBox綁定到視圖模型的字符串屬性。我選擇ComboBox而不是TextBox,因爲我想要從列表中選擇一個選項(作爲建議),但如果ItemsSource更改,我不想更改所選文本。如何使用ComboBox的Text屬性禁用ItemsSource同步
我試圖設置IsSynchronizedWithCurrentItem屬性爲false,但是當建議列表更改(在選定文本的位置)時,文本更改爲空。 似乎ComboBox已經記住輸入的文本也在列表中,當這個項目消失時,Text屬性也被清除。
所以我的問題是:這是一個錯誤,或者我做錯了什麼? 如果這是一個錯誤,你能提出一些解決辦法嗎?
我創建了preproduces這個示例項目:
在XAML:
<Window x:Class="TestProject1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Items}"
IsEditable="True" Text="{Binding SelectedText, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" Margin="10,39,0,0" VerticalAlignment="Top" Width="120"/>
<Button Click="Button_Click" Content="Update list"
HorizontalAlignment="Left" Margin="10,82,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
在後面的代碼:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow() {
InitializeComponent();
this.DataContext = this;
Items = new List<string>() { "0", "1", "2" };
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private List<string> _items;
public List<string> Items {// I use IEnumerable<string> with LINQ, but the effect is the same
get { return _items; }
set {
if (_items != value) {
_items = value;
RaisePropertyChanged("Items");
}
}
}
private string _selectedText;
public string SelectedText {
get { return _selectedText; }
set {
if (_selectedText != value) {
_selectedText = value;
RaisePropertyChanged("SelectedText");
}
}
}
private void Button_Click(object sender, RoutedEventArgs e) {
var changed = Items.ToList();//clone
int index = changed.IndexOf(SelectedText);
if (index >= 0) {
changed[index] += "a";//just change the currently selected value
}
Items = changed;//update with new list
}
}
我試了一下,並沒有奏效。我不知道它爲什麼要工作? – user3387366
它應該工作,因爲當ItemsSource更改SelectedItem將被清除的概念時 - 通過RaisingPropertyChanged選定的項目,您可以重新選擇哪個項目。但我剛剛注意到你正試圖改變所選項目的*文本*,並以某種方式保留依賴於文本的綁定......這將無法工作。我會編輯我的答案。 – Mashton
首先,我想從列表中選擇文字。然後,我不希望在更改項目源後更改所選文本。如果集合(字符串)的類型是引用類型,那麼這是一個預期的行爲。但是字符串是一個值類型,因此在選擇它之後應該保持它被選中的狀態(新副本)。 – user3387366