MVVM:在列表视图中没有更新绑定值
我有列表视图中显示的对象列表,并且当另一个复选框被选中时,我想更新其中一个对象的值。然后我希望显示该值的文本框用这个新值更新。 目前我已经能够获得持有其他值的对象,并增加它,但不显示新的值。MVVM:在列表视图中没有更新绑定值
<ListView Margin="0,5,5,5" Name="TruckModels" DockPanel.Dock="Top" Height="400" Width="1310" ItemsSource="{Binding ListOfTrucks}" HorizontalAlignment="Right">
<ListView.View>
<GridView>
<!--New Version column-->
<GridViewColumn Header="New Version" Width="105">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBox AcceptsReturn="True" Height="Auto" Width="80" IsReadOnly="False"
x:Name="CurrentVersionTB" TextAlignment="Left" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
Text="{Binding NewVersionVal}"
Foreground="#FFCD0000"
DataContextChanged="CurrentVersionTB_DataContextChanged"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<!--Auto-Increment Version? column-->
<GridViewColumn Header="Auto-Increment Version?" Width="105">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<CheckBox IsChecked="{Binding IsSelectedAINV}" Checked="AutoIncrement_Checked" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
CS代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//List<Truckinfo> items = new List<Truckinfo>();
ObservableCollection<Truckinfo> items = new ObservableCollection<Truckinfo>();
xmlparsing(items);
TruckModels.ItemsSource = items;
}
private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
{
ScrollViewer scrollviewer = sender as ScrollViewer;
if (e.Delta > 0)
{
scrollviewer.LineUp();
}
else
{
scrollviewer.LineDown();
}
e.Handled = true;
}
private void AutoIncrement_Checked(object sender, RoutedEventArgs e)
{
Truckinfo item = (Truckinfo)(sender as CheckBox).DataContext;
int i;
int.TryParse(item.NewVersionVal, out i);
i++;
item.NewVersionVal = i.ToString();
Debug.WriteLine(item.NewVersionVal);
e.Handled = true;
}
private void CurrentVersionTB_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox t = sender as TextBox;
t.BindingGroup.UpdateSources();
}
}
我曾尝试使用INCP接口,但它不希望填充表的对象。
ObservableCollection
只在集合发生更改时才更新,而不是项目更改时更新。
您将必须有TruckInfo
实施INotifyPropertyChanged
为单个项目更改自动更新视图。
或者,您可以从集合中删除项目,然后重新插入项目。然后,您将触发收藏更改事件,并且该视图将更新以匹配最新的收藏内容。
谢谢我将实施INotifyPropertyChanged。我之前尝试过,但它没有用我的卡车对象列表填充我的列表视图。 – Kennya42
这是一个隐藏的陷阱。项目和集合都需要Observation/INotifyPropertyChanged以使视图完全响应。 –
请转告您Trucktern类。也许它需要实现'INotifyPropertyChanged'。 –
'Truckinfo'没有实现'INotifyPropertyChanged',那是你在说什么? *“它不想用对象填充表格”*几乎可以表示任何事物,因为我无法猜测“它”,“表格”或“对象”是指什么。你能描述一下实际问题吗?你会惊讶于这个世界上有多少东西可以用“it”来形容。基本上所有这些。所以很难缩小范围。我的意思是,有很多东西在我的桌子上可见。 –