简体   繁体   English

当源数据更改时,如何更新数据绑定组合框?

[英]How to update a data bound combobox when the source data changes?

I'm working on a C# Metro style app for Windows 8, and I'm having a problem getting my data bound combo box to update when the source data changes. 我正在为Windows 8开发C#Metro风格的应用程序,但是在源数据更改时无法更新数据绑定组合框时遇到了问题。

Here's the data source: 这是数据源:

public class Range
{
    public string range_name { get; set; }
    public string range_description { get; set; }
    public int min { get; set; }
    public int max { get; set; }
}

static List<Range> ranges = new List<Range>
{
    new Range { range_name = "Foo", range_description = "Foo: (0-10)", min = 0, max = 10},
    new Range { range_name = "Bar", range_description = "Bar: (5-15)", min = 5, max = 15},
    new Range { range_name = "Baz", range_description = "Baz: (10-20)", min = 10, max = 20},
    new Range { range_name = "Custom", range_description = "Custom: (0-20)", min = 0, max = 20}
};

And the combo box: 和组合框:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ComboBox Name="combo_range" ItemsSource="{Binding Path=Range}" DisplayMemberPath="range_description" SelectedValuePath="range_name" SelectedValue="{Binding Path=Range}" SelectionChanged="combo_range_SelectionChanged"/>
</Grid>

The user can manipulate the min/max range range of the 'custom' range in the app, but the combo box only updates when I change away from that record and then change back to 'custom'. 用户可以在应用程序中操纵“自定义”范围的最小/最大范围,但是组合框仅在我更改该记录然后又变回“自定义”时更新。

What do I need to do to force the combo box to update in real time when the source data changes? 当源数据更改时,我该怎么做以强制组合框实时更新?

Thanks 谢谢

Try implementing INotifyPropertyChanged in your Range class: 尝试在Range类中实现INotifyPropertyChanged

public class Range : INotifyPropertyChanged
{
    // Declare the event
    public event PropertyChangedEventHandler PropertyChanged;
    private string _range_name;
    private string _range_description;
    private int _min;
    private int _max;

    public string range_name
    {
        get { return this._range_name; }
        set
        {
            _range_name = value;
            OnPropertyChanged("range_name");  
        }  // Call OnPropertyChanged whenever the property is updated
    }
    public string range_description
    {
        get { return this._range_description; }
        set
        {
            _range_description = value;
            OnPropertyChanged("range_description");
        }
    }
    public int min
    {
        get { return this._min; }
        set
        {
            _min=value;
            OnPropertyChanged("min");
        }
    }
    public int max
    {
        get { return this._max; }
        set
        {
            _max = value;
            OnPropertyChanged("max");
        }
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

Hope it helps ;) 希望能帮助到你 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM