【发布时间】:2015-06-01 09:49:46
【问题描述】:
我有一个包含 Hour(对象)的 ObservableCollection。在里面,我有一个 Title 和一个 Value 属性。
在我看来,我有一个列表视图,绑定在这个集合上。 Title 是文本块,Value 是文本框(用户可以输入文本)。
我想在一次更改时更改所有文本框(值)的内容。 一点代码:
public class Hour : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public string Title { get; set; }
private int valueContent;
public int Value
{
get { return valueContent; }
set
{
valueContent = value;
NotifyPropertyChanged("Value");
}
}
}
我的可观察集合:
private ObservableCollection<Hour> hours;
public ObservableCollection<Hour> Hours
{
get { return hours; }
set
{
hours= value;
NotifyPropertyChanged("Hours");
}
}
xaml:
<ListBox Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="3" Grid.RowSpan="3" ItemsSource="{Binding Hours, Mode=TwoWay}" SelectedItem="{Binding SelectedHour,Mode=TwoWay}" ItemTemplate="{StaticResource HourTemplate}" />
<DataTemplate x:Key="HourTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" FontSize="18" Width="150" />
<TextBox Text="{Binding Value, Mode=TwoWay}" FontSize="15" Width="150" TextChanged="TextBox_TextChanged" />
</StackPanel>
</DataTemplate>
所以,我会举个例子:
Title - Value
08h00 - 0
09h00 - 0
10h00 - 0
11h00 - 0
12h00 - 0
我希望,当我更改一个值(例如:10h00)时,该值之后的所有值都更改为 10h00 的值。 这是预期的结果:
Title - Value
08h00 - 0
09h00 - 0
10h00 - 1 <--- change here
11h00 - 1 <--- change because 10h00 changed
12h00 - 1 <--- change because 10h00 changed
感谢您的帮助。
【问题讨论】: