【发布时间】:2014-11-05 14:06:23
【问题描述】:
我的 View 中有 ListView,其 itemsSource 绑定到 Observable 集合。这是我的看法
<ListView x:Name="Details"
ItemsSource="{Binding Details}"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="3">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="White" Width="350">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="175"/>
<ColumnDefinition Width="175"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="{Binding Key}"/>
<TextBox Grid.Row="0"
Grid.Column="1"
Text="{Binding Value}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
问题:当我运行我的应用程序并更改我的 TextBox 值时,如何在我的 viewModel 中获得一个仅具有这些更改的新集合?我想要一个我只更改过的值的集合。目前,我得到了一组我已更改的值和旧值。这是我的 MainViewModel:
private ObservableCollection<Details> _details;
public ObservableCollection<Details> Details
{
get { return _details; }
set
{
_details= value;
RaisePropertyChanged();
}
}
public MainViewModel()
{
Details= new ObservableCollection<Details>
{
new Details() {Key = "age", Value = 25},
new Details() {Key = "sex", Value = "female"},
new Details() {Key = "height", Value = 3}
};
}
细节类:
public class Details
{
public string Key { get; set; }
public object Value { get; set;}}
在上面的代码中,如果我将年龄更改为 20,,我将得到一个集合:(年龄 20,性别女性,身高 3) 但我只想要(20 岁),即我只更改了 textBox 的 Details 对象!!。我不想比较原始收藏和新收藏来获得它。有什么办法可以触发一个事件或那些很长的东西。
谢谢
【问题讨论】:
-
完全是VM逻辑。您应该订阅每个集合值的 PropertyChanged 事件,并在处理程序中将作为事件源的项目放入单独的集合中。