是否有可能再次调用ObservableCollection
get方法?我需要改变基于ItemsSource
选择ComboBox
所以我需要调用我的ObservableCollection
再次,这是我的代码
ObservableCollection<string> sampleData = new ObservableCollection<string>();
public ObservableCollection<string> SampleData
{
get
{
if (sampleData.Count < 1)
sampleData.Add(line);
return sampleData;
}
}
上面的代码运行一次,当应用程序luanch,但我需要的时候调用它改变组合框
private void CmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (cmbFilter.SelectedIndex)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
一个ObservableCollection
整点是,你并不需要实现你在你的SampleData
吸气剂暗示什么。还有你可以采取这里2点的方法:
利用ObservableCollection
的
注意,有更好的选择,在这里,但出了问题的范围。查找了SelectedItem
绑定一个ComboBox
。
private void CmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Modify your collection here based on what was selected.
}
忘掉ObservableCollection
这种方法要求你实现INotifyPropertyChanged
您ViewModel类。
public IEnumerabe<string> SampleData
{
get
{
// Return values based on the selection.
if (SelectedData == "FirstValueICareAbout") // SelectedData assumes you have investigated how to bind to the SelectedItem of a ComboBox.
{
return new[]
{
"FirstValue",
"SecondValue"
};
}
return Enumerable.Empty<string>();
}
}
private void CmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Notify the UI that SampleData has changed using INotifyPropertyChanged implementation.
RaiseNotifyPropertyChanged(nameof(SampleData));
}