[英]XAML binding not firing on a model implementing INPC
我有一组可观察的模型。 这些模型实现了INPC在包含集合的List的数据模板中,我设置了该项目的属性,以在单击列表项时更改。 触发on属性更改事件,但XAML从未更新。 请问有人有解决办法吗?
//The observable collection
private ObservablePagedList<ProposalModel> _proposals;
public ObservablePagedList<ProposalModel> Proposals
{
get => _proposals;
set => this.RaiseAndSetIfChanged(ref _proposals, value);
}
在XAML内,我设置了一个带有按钮的集合视图,该按钮的属性根据绑定到该项目的属性而变化。
<CollectionView ItemsSource="{Binding Proposals}" Margin="0,10,0,0"> <CollectionView.ItemTemplate> <DataTemplate> ... ... <Button Text="{markupExtensions:Translate Accept}" Command="{Binding Path=BindingContext.AcceptProposalCommand, Source={x:Reference _myAnnDetailsPageklas}}" CommandParameter="{Binding}"> <Button.Triggers> <DataTrigger TargetType="Button" Binding="{Binding Path=State, Mode=TwoWay}" Value="{x:Static models:ProposalStates.Approved}"> <Setter Property="Text" Value="{markupExtensions:Translate Reject}"/> <Setter Property="Command" Value="{Binding Source={x:Reference _myAnnDetailsPageklas}, Path=BindingContext.RejectProposalCommand}"/> </DataTrigger> </Button.Triggers> </Button> ... ... </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
Collection实现的INPC中的每个模型,当我订阅PropertyChangedEvent时,都会在模型的属性更改时触发。
当用户“单击按钮以接受建议时,将触发” AcceptProposal命令,以下是该命令的内容:”
async Task AcceptProposal(ProposalModel proposal)
{
try
{
IsBusy = true;
//这是更改按钮被单击的模型状态的地方。 但是更改不会反映在UI Proposals.Where(prop => prop.Id == proposal.Id).FirstOrDefault()。State = ProposalStates.Approved;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw;
}
finally
{
IsBusy = false;
}
}
您的DataTrigger设置程序没有任何作用,因为您已经在按钮上设置了优先于触发器的本地值。
您需要改用样式设置默认值
<Button CommandParameter="{Binding}">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Text" Value="{markupExtensions:Translate Accept}"/>
<Setter Property="Command" Value="{Binding Path=BindingContext.AcceptProposalCommand, Source={x:Reference _myAnnDetailsPageklas}}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State}" Value="{x:Static models:ProposalStates.Approved}">
<Setter Property="Text" Value="{markupExtensions:Translate Reject}"/>
<Setter Property="Command" Value="{Binding Source={x:Reference _myAnnDetailsPageklas}, Path=BindingContext.RejectProposalCommand}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button>
ps。 您是在按钮文本中表示“文本”还是“内容”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.