首页 > 解决方案 > 更新视图不​​起作用(通过绑定属性)

问题描述

我的意图是在 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);
            }
        }
    }

标签: c#wpfcaliburn

解决方案


推荐阅读