刷新 Caliburn Micro WPF 中 属性 更改的视图
Refresh view on property change in Caliburn Micro WPF
得到一个视图,其中有一个 BindingList 属性。这负责存储工作项,添加和删除。后端工作正常,但 UI 未更新。
观点:
<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>
属性:
public BindingList<WorkPieceModel> WorkPieces
{
get { return _workPieces; }
set
{
_workPieces = value; NotifyOfPropertyChange(() => WorkPieces);
NotifyOfPropertyChange(() => CanCalculate);
}
}
更新显示名称和设置图标显示标志的后端函数:
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);
}
因此,当我单击 Add/Remove 时,它会正确更新元素的数量,但其余部分不会刷新按钮或 DisplayNumber。
尝试在添加和移除工件后调用 NotifyOfPropertyChange(),但没有任何预期结果。
目标是仅在最后一个元素上显示 + - 图标,其余元素显示 - 图标,显示的数字始终按升序排列,而不管哪个元素已被删除。
如你所见:IV.失踪和三。有 + 图标(不应该有)
由于您要绑定到 IsLast
,因此您应该在 WorkPieceModel
class 中实现 INotifyPropertyChanged
接口,并在 PropertyChanged
中引发 PropertyChanged
事件=18=] 共 IsLast
。
得到一个视图,其中有一个 BindingList 属性。这负责存储工作项,添加和删除。后端工作正常,但 UI 未更新。
观点:
<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>
属性:
public BindingList<WorkPieceModel> WorkPieces
{
get { return _workPieces; }
set
{
_workPieces = value; NotifyOfPropertyChange(() => WorkPieces);
NotifyOfPropertyChange(() => CanCalculate);
}
}
更新显示名称和设置图标显示标志的后端函数:
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);
}
因此,当我单击 Add/Remove 时,它会正确更新元素的数量,但其余部分不会刷新按钮或 DisplayNumber。 尝试在添加和移除工件后调用 NotifyOfPropertyChange(),但没有任何预期结果。
目标是仅在最后一个元素上显示 + - 图标,其余元素显示 - 图标,显示的数字始终按升序排列,而不管哪个元素已被删除。
如你所见:IV.失踪和三。有 + 图标(不应该有)
由于您要绑定到 IsLast
,因此您应该在 WorkPieceModel
class 中实现 INotifyPropertyChanged
接口,并在 PropertyChanged
中引发 PropertyChanged
事件=18=] 共 IsLast
。