我有一個使用ObservableCollection作爲源的組合框。我已綁定作爲源如下綁定到ObservableCollection的組合框不會更新
<ComboBox IsEditable="False"
SelectedIndex="{Binding Source={x:Static Properties:CollectionControl.Settings}, Path=SamplingPeriodIndex, Mode=TwoWay}"
SelectionChanged="onPeriodControlSelectionChanged"
Name="PeriodControl"
ItemsSource="{StaticResource test}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SamplingPeriod}" Visibility="{Binding Converter={StaticResource TrackVis}, ConverterParameter=GroupIndex}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
TrackVis是確定如果元素是可見的或摺疊,這取決於已INotifyPropertyChanged的實現的外部特性的轉換器。
一切正常,第一次ComboBox顯示,但組合框永遠不會刷新,以反映變化。我一定錯過了一些東西,但截至目前,我所嘗試過的一切都失敗了。
這裏是轉換器
public class IsVisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
var tempObj = (SamplingPeriods) value;
if (tempObj.GroupIndex >= CollectionControl.Settings.SamplingFrequencyIndex)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
另外的代碼,這裏是收集
public class PeriodsCollection : ObservableCollection<SamplingPeriods>
{
public PeriodsCollection()
{
Add(new SamplingPeriods("1/16 of a second", 13));
Add(new SamplingPeriods("1/8 of a second", 12));
Add(new SamplingPeriods("1/4 of a second", 11));
Add(new SamplingPeriods("1/2 of a second", 10));
Add(new SamplingPeriods("1 second", 9));
Add(new SamplingPeriods("2 seconds", 8));
Add(new SamplingPeriods("4 seconds", 7));
Add(new SamplingPeriods("8 seconds", 6));
Add(new SamplingPeriods("16 seconds", 5));
Add(new SamplingPeriods("32 seconds", 4));
Add(new SamplingPeriods("64 seconds", 3));
Add(new SamplingPeriods("128 seconds", 2));
Add(new SamplingPeriods("256 seconds", 1));
Add(new SamplingPeriods("512 seconds", 0));
}
}
public class SamplingPeriods
{
public SamplingPeriods(string samplingPeriod, int groupIndex)
{
SamplingPeriod = samplingPeriod;
GroupIndex = groupIndex;
}
public string SamplingPeriod { get; private set; }
public int GroupIndex { get; private set; }
}
的想法是,所選擇的採樣頻率限制了可用的採樣週期。採樣頻率指數範圍從0到11.例如,如果採樣索引是9,則只有有效採樣週期將具有GroupIndex> = 9.其他採樣週期將被摺疊。
爲什麼靜態資源,而不是簡單地命名的財產? – sll
對TrackVis或selectedindex或兩者的更改? – Tyrsius
當問這樣的問題時,確實非常重要的是要具體說明什麼不是更新。將新項目添加到集合時,新項目是否不會出現在「ComboBox」中?當您更改代碼中的綁定屬性時,「SelectedIndex」不會更新嗎?具體而言,現在和現在都沒有發生什麼? –