首页 > 解决方案 > 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;
        }
    }

标签: c#xamlxamarin.formsdata-binding

解决方案


您的 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>

附言。您是指按钮文本的“文本”还是“内容”?


推荐阅读