我正在为
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)",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>
用户可以操作应用程序中“自定义”范围的最小/最大范围范围,但组合框仅在我更改远离该记录然后更改回“自定义”时更新.
当源数据发生变化时,我需要做什么来强制实时更新组合框?
谢谢
解决方法
尝试在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)); } } }
希望能帮助到你
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。