c# - 更新视图不起作用(通过绑定属性)
问题描述
我的意图是在 Shippers 集合或其任何成员被修改时更新 wpf 窗口。托运人集合及其修改工作正常,但视图没有更新,尽管我认为我正确设置了所有内容。
请问,有人可以看到为什么视图没有通过 Shippers 集合修改正确更新的原因吗?
XAML:
<Window x:Class="EnterEventTextBox.DataView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EnterEventTextBox"
mc:Ignorable="d" Background="Black"
Title="DataView" Height="450" Width="300"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cal="http://www.caliburnproject.org"
>
<Window.Resources>
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
<WrapPanel Orientation="Horizontal" IsItemsHost="True" Background="Turquoise"/>
</ItemsPanelTemplate>
<Style x:Key="ItemsControlStyle1" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ItemsControl ItemsSource="{Binding Shippers}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Style="{DynamicResource ItemsControlStyle1}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<WrapPanel Background="Green" Orientation="Horizontal">
<ToggleButton Height="50" Width="50" Background="Red" Content="{Binding BtnLabelShipper}" Margin="0,0,5,5"
cal:Message.Attach="[Event Click] = [Action ShipperIsClicked($dataContext, $this.IsChecked)]">
</ToggleButton>
<ItemsControl ItemsSource="{Binding Parcels}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Button Height="50" Width="50" Background="Red" Content="{Binding ParcelNumber}" Margin="0,0,5,5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- -->
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
查看型号:
public partial class DataViewModel : Screen
{
public DataViewModel()
{
Shippers.Add(new Shipper() { ParcelAmount = 5, ShipperName = "PPLppl", IsUnrolled = true});
Shippers.Add(new Shipper() { ParcelAmount = 7, ShipperName = "DPDdpd", IsUnrolled = false });
Shippers.Add(new Shipper() { ParcelAmount = 9, ShipperName = "GEISgeis", IsUnrolled = false });
}
public void ShipperIsClicked(Shipper shp, bool isChecked)
{
if (shp != null)
{
shp.IsUnrolled = isChecked;
NotifyOfPropertyChange(nameof(Shippers));
}
}
private List<Shipper> shippers = new List<Shipper>();
public List<Shipper> Shippers
{
get { return shippers; }
set
{
shippers = value;
NotifyOfPropertyChange(() => Shippers);
}
}
}
解决方案
推荐阅读
- python - 二元分类的准确性评估存在疑问
- angular - 带有 mat-table 的嵌套数组
- git - 在 git 中处理大 blob 的最佳方法是什么?
- c++ - 延迟随着显示时间 + 记录管道的增加而增加
- reactjs - 反应胜利图表:胜利工具提示不起作用
- css - CSS 要么在一行中显示所有元素,要么在新行中显示每个元素
- r - 如何在网络分析中处理空值/NA
- python - 在这个代码示例中,什么是大 O,O(N) 或 O(N^2)?
- java - macOS Mojave:Android 工作室:AVD 的模拟器进程被杀死
- vue.js - 为什么显示(读取)数据会改变 vue.js 中输入(滑块)的值?