【问题标题】:How to update a data bound combobox when the source data changes?源数据更改时如何更新数据绑定组合框?
【发布时间】:2012-07-29 02:44:52
【问题描述】:

我正在开发适用于 Windows 8 的 C# Metro 风格应用程序,但在源数据更改时更新我的​​数据绑定组合框时遇到问题。

这是数据源:

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

还有组合框:

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

用户可以在应用程序中操纵“自定义”范围的最小/最大范围范围,但组合框仅在我从该记录更改然后更改回“自定义”时才会更新。

当源数据发生变化时,我需要怎么做才能强制组合框实时更新?

谢谢

【问题讨论】:

    标签: c# data-binding combobox windows-8 microsoft-metro


    【解决方案1】:

    尝试在 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));
            }
        }
    }
    

    希望对你有帮助;)

    【讨论】:

    • 非常感谢您的建议。我尝试了这两种方法,但我认为它们不会在 Metro 风格的应用程序中工作。 BindingExpression 和 GetDefaultView 在 System.Windows.Data 中有效,但在 Metro 等效 Windows.UI.Xaml.Data 中无效
    • 谢谢 - 这正是我所需要的,并且适用于我的 Metro 风格应用程序。
    • 很高兴听到您的问题得到解决。我建议你阅读OneWay and TwoWay binding modes :-)
    • 我只想添加它,而不是手动实现 INotifyPropertyChanged,考虑从 BindableBase 继承(包含在 Grid App 和 Split App 模板项目的 Common 文件夹中)。 BindableBase 使添加带有通知的属性变得更加简单,并解决了将带引号的字符串传递给 OnPropertyChanged 的​​需要(带引号的字符串无法正确重构)。
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2015-12-17
    • 2022-01-19
    相关资源
    最近更新 更多