【问题标题】:XAML binding not firing on a model implementing INPCXAML 绑定未在实现 INPC 的模型上触发
【发布时间】: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


    【解决方案1】:

    您的 DataTrigger 设置器无效,因为您已经在 take precedence 触发的按钮上设置了本地值。

    您需要在样式中设置默认值

    <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。您是指按钮文本的“文本”还是“内容”?

    【讨论】:

    • 谢谢,但这更糟,现在当我运行应用程序时,按钮是空的,没有文本,并且命令未绑定
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多