【发布时间】:2019-09-17 09:28:22
【问题描述】:
我有一个可观察的模型集合。 这些模型实现了 INPC 在包含集合的 List 的数据模板中,我设置了该项目的属性以在单击列表项时更改。 on property changed 事件被触发,但 XAML 永远不会更新。 请问有人有解决办法吗?
//The observable collection
private ObservablePagedList<ProposalModel> _proposals;
public ObservablePagedList<ProposalModel> Proposals
{
get => _proposals;
set => this.RaiseAndSetIfChanged(ref _proposals, value);
}
在 XAML 中,我设置了一个带有按钮的集合视图,该按钮的属性会根据它所绑定的项目的 a 属性而改变。
<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;
}
}
【问题讨论】:
标签: c# xaml xamarin.forms data-binding