简体   繁体   English

刷新Caliburn Micro WPF中的属性更改视图

[英]Refresh view on property change in Caliburn Micro WPF

Got a view, which has a BindingList property. 有一个视图,它具有BindingList属性。 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. 因此,当我单击“添加/删除”时,它会正确更新元素的数量,但其余元素不会刷新按钮或DisplayNumber。 Tried to call NotifyOfPropertyChange() after adding and removing workpiece, without any desired result. 尝试在添加和移除工件之后调用NotifyOfPropertyChange(),但没有任何期望的结果。

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. 如您所见:IV。 is missing and III. 失踪了。 has + icon (it shouldn't have) 有+图标(不应包含) 来自问题的图片

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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