简体   繁体   中英

Refresh view on property change in Caliburn Micro WPF

Got a view, which has a BindingList property. This is responsible to store workitems, add and remove. The backend is working fine, but the UI is not updated.

The view:

<ListBox Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" x:Name="WorkPieces" HorizontalAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="1" x:Name="DisplayName" Text="{Binding DisplayName}" MinWidth="40
                           " FontWeight="Bold"/>
                <TextBlock Grid.Column="2" Text="{x:Static lang:Resources.Txt_W}" />
                <TextBox Grid.Column="3" x:Name="Width" MinWidth="50" Text="{Binding Width}" TextAlignment="Right"/>
                <TextBlock Grid.Column="4" Text=" x " />
                <TextBlock Grid.Column="5" Text="{x:Static lang:Resources.Txt_L}" />
                <TextBox Grid.Column="6" x:Name="Length" MinWidth="50"  Text="{Binding Length}" TextAlignment="Right"/>

                <Button Grid.Row="0" Grid.Column="7" Margin="5" BorderThickness="0" Background="Transparent"
                        Visibility="{Binding IsLast, Converter={StaticResource Converter}}">
                    <Image Source ="/Images/plus-sign.png" Height="16" Width="16" />
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <cal:ActionMessage MethodName="AddNewWorkPiece" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>

                <Button Grid.Row="0" Grid.Column="8" Margin="5" BorderThickness="0" Background="Transparent">
                    <Image Source ="/Images/minus-sign.png" Height="16" Width="16" />
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <cal:ActionMessage MethodName="RemoveWorkPiece">
                                <cal:Parameter Value="{Binding Id}" />
                            </cal:ActionMessage>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The property:

public BindingList<WorkPieceModel> WorkPieces
{
    get { return _workPieces; }
    set
    {
        _workPieces = value; NotifyOfPropertyChange(() => WorkPieces);
        NotifyOfPropertyChange(() => CanCalculate);
    }
}

The backend function to update display name and set icon display flags:

private void UpdateWorkPiecesDisplayName()
{
    var counter = 1;
    foreach (var item in WorkPieces)
    {
        item.DisplayName = Roman.To(counter);
        item.IsLast = false;
        counter++;
    }

    WorkPieces.Last().IsLast = true;
    NotifyOfPropertyChange(() => WorkPieces);
}

So when I click on the Add/Remove it is updating properly the number of elements, but the rest does not refreshing the buttons or the DisplayNumber. Tried to call NotifyOfPropertyChange() after adding and removing workpiece, without any desired result.

The goal is to display a + - icon only on the last element, - icon for the rest, and the displayed numbers always be ascending, disregarding which element has been removed.

As you can see: IV. is missing and III. has + icon (it shouldn't have) 来自问题的图片

既然你绑定到IsLast ,应实现INotifyPropertyChanged的接口WorkPieceModel类和提高PropertyChanged中的二传手事件IsLast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM