绑定到ObservableCollection的ComboBox不会更新 [英] ComboBox bound to a ObservableCollection does not update

查看:156
本文介绍了绑定到ObservableCollection的ComboBox不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ObservableCollection作为源的ComboBox。我有源绑定如下

 < ComboBox IsEditable =False
SelectedIndex ={Binding Source = {x:静态属性: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时,一切都会按预期运行,但ComboBox不会刷新以反映更改。我必须缺少一些东西,但是到目前为止,我尝试过的一切都失败了。



这是转换器的代码



公共类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 =取样周期
GroupIndex = groupIndex;
}

public string SamplingPeriod {get;私人集合}
public int GroupIndex {get;私人集合}
}

想法是选定的采样频率限制了可用的采样周期。采样频率指数范围从0到11.例如,如果采样指数为9,则唯一有效的采样周期将具有GroupIndex> =
9.其他采样周期将被折叠。

解决方案

您正在尝试跟踪采样频率索引。那么你必须绑定到具有这样的属性的对象,并且实现INotifyPropertyChanged.Or,如我已经说过的那样,把这个事件传播给你的绑定源的对象,并提出正确的propertychanged。否则,绑定引擎将不会知道该属性的更改。
绑定到 CollectionControl.Settings Path = SamplingFrequencyIndex


I have a ComboBox that uses a ObservableCollection as the source. I have the source bound as follows

<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 is a converter that determines if the element is visible or collapsed depending on an external property which has INotifyPropertyChanged implemented.

Everything works as expected the first time the ComboBox is displayed, but the ComboBox is never refreshed to reflect changes. I must be missing something, but as of now everything I have tried fails.

Here is the code for the converter

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();
    }
}

Also, here is the collection

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; }
}

The idea is that the selected sampling frequency limits the sampling periods that are available. The sampling frequency index ranges from 0 to 11. For example, if the sampling index is 9 the only valid sampling periods would have a GroupIndex >= 9. The other sampling periods would be collapsed.

解决方案

You are trying to track samplingfrequency index. then you must bind to an object that have such property and implements INotifyPropertyChanged.Or, as I have already said, propagate this event to the object that is your binding source, and raise correct propertychanged on it. Otherwise, binding engine would know nothing of the changes of that property. Bind to the CollectionControl.Settings with a Path = SamplingFrequencyIndex

这篇关于绑定到ObservableCollection的ComboBox不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆