更新视图不​​起作用(通过绑定属性)

时间:2018-08-16 12:31:44

标签: c# wpf caliburn

我的目的是每当修改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);
            }
        }
    }

0 个答案:

没有答案