c# - XAML 绑定未在实现 INPC 的模型上触发
问题描述
我有一个可观察的模型集合。这些模型实现了 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 中,我设置了一个带有按钮的集合视图,该按钮的属性根据它所绑定到的项目的属性而改变。
<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>
附言。您是指按钮文本的“文本”还是“内容”?
推荐阅读
- javascript - 两天(含)之间的 Javascript/Angular 天数
- javascript - 有什么方法可以检查“从不输入”并引发错误或警告吗?
- python - 具有 fit_transform 错误的列转换器
- kotlin - 如何在数据类中启动固定大小的数组
- api - 使用 Alloy API 生成与模型实例对应的 .als 文件
- java - Spring Data MongoDB中@MongoId在@Id上的用途是什么?
- python - 为 OLS 的预测计算学生化删除残差和 outlier_test()
- javascript - 传单导入 GeoJson
- android - 为什么服务方法 OnCreate 调用了两次?
- javascript - 在reduce()中跳过嵌套数组中的空数组