在后面的代码中再次调用 ObservableCollection 获取访问器
call ObservableCollection get accessor again in code behind
是否可以再次调用ObservableCollection
get方法?
我需要根据 ComboBox
选择更改 ItemsSource
所以我需要再次调用我的 ObservableCollection
这是我的代码
ObservableCollection<string> sampleData = new ObservableCollection<string>();
public ObservableCollection<string> SampleData
{
get
{
if (sampleData.Count < 1)
sampleData.Add(line);
return sampleData;
}
}
上面的代码运行在应用程序崩溃时调用一次,但我需要在组合框更改时调用它
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
getter 中建议的内容。您可以在此处采用 2 种方法:
利用ObservableCollection
请注意,这里有更好的选择,但超出了问题的范围。为 ComboBox
.
查找 SelectedItem
绑定
private void CmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Modify your collection here based on what was selected.
}
忘了ObservableCollection
此方法需要您在 ViewModel class.
上实施 INotifyPropertyChanged
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));
}
是否可以再次调用ObservableCollection
get方法?
我需要根据 ComboBox
选择更改 ItemsSource
所以我需要再次调用我的 ObservableCollection
这是我的代码
ObservableCollection<string> sampleData = new ObservableCollection<string>();
public ObservableCollection<string> SampleData
{
get
{
if (sampleData.Count < 1)
sampleData.Add(line);
return sampleData;
}
}
上面的代码运行在应用程序崩溃时调用一次,但我需要在组合框更改时调用它
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
getter 中建议的内容。您可以在此处采用 2 种方法:
利用ObservableCollection
请注意,这里有更好的选择,但超出了问题的范围。为 ComboBox
.
SelectedItem
绑定
private void CmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Modify your collection here based on what was selected.
}
忘了ObservableCollection
此方法需要您在 ViewModel class.
上实施INotifyPropertyChanged
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));
}