我有一个使用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”不会更新吗?具体而言,现在和现在都没有发生什么? –