【发布时间】:2014-02-04 19:04:51
【问题描述】:
我有一个绑定到 ObservableCollection 对象(具有多个属性)的 ComboBox。组合框准确地显示所有对象的所需属性,我可以按预期从组合中选择任何项目。
<ComboBox Height="23" Name="comboBox1" Width="120" Margin="5" ItemsSource="{Binding Issues}" DisplayMemberPath="Issue" SelectedValuePath="Issue" SelectedValue="{Binding Path=Issues}" IsEditable="False" SelectionChanged="comboBox1_SelectionChanged" LostFocus="comboBox1_LostFocus" KeyUp="comboBox1_KeyUp" Loaded="comboBox1_Loaded" DropDownClosed="comboBox1_DropDownClosed" IsSynchronizedWithCurrentItem="True" />
我有一系列文本框,它们应该显示所选对象的其他属性。这也很好用。
<TextBox Height="23" Name="textBox5" Width="59" IsReadOnly="True" Text="{Binding Issues/LastSale, StringFormat={}{0:N4}}" />
<TextBox Height="23" Name="textBox9" Width="90" IsReadOnly="True" Text="{Binding Path=Issues/LastUpdate, Converter={StaticResource TimeConverter}}" />
但是... ObservableCollection 的属性会定期在 Code-Behind 中更新,并且每次更新属性时,我都会通过在其中添加或删除一个虚拟对象来更改 OC。 (我发现这比其他解决方案更简单)。
但是...文本框中的数据不会改变! :-( 如果我从 ComboBox 中选择不同的对象,我会得到更新的信息,但在更改 OC 时它不会更改。
OC 是由一堆这样的 Objects 组成的:
public class IssuesItems
{
public String Issue { get; set; }
public Double LastSale { get; set; }
public DateTime LastUpdate { get; set; }
...
}
OC定义为:
public ObservableCollection<IssuesItems> Issues { get; set; }
并实例化:
this.Issues = new ObservableCollection<IssuesItems>();
我做错了什么?我读到的所有内容都表明,当 OC 中的 LastSale 和 LastUpdate 属性发生变化时(并且我做了一些事情来强制更新 OC),文本框中的数据应该会发生变化。
【问题讨论】:
-
每次
ObservableCollection更新时,您是否都在引发propertychanged事件?
标签: c# wpf data-binding combobox observablecollection