简体   繁体   中英

XAML binding not firing on a model implementing INPC

I have an observable collection of models. these models implement INPC Inside the data templates of the List which contains the collection, I set a property of the item to change when the list item is clicked. The on property changed event is fired, but the XAML is never updated. Does someone have a solution please ?

//The observable collection
private ObservablePagedList<ProposalModel> _proposals;

        public ObservablePagedList<ProposalModel> Proposals
        {
            get => _proposals;
            set => this.RaiseAndSetIfChanged(ref _proposals, value);
        }

Inside the XAML, I set a collection view with a button whose property change depending on the a property of the item to which it is binded.

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

Every model inside the Collection implement's INPC and when I subscribe to the PropertyChangedEvent, it fires when the property of the model is changed.

When the user "Clicks the Button, to Accept a proposal, the "AcceptProposal command is fired, and here is the content of that command:"

async Task AcceptProposal(ProposalModel proposal)
    {
        try
        {
            IsBusy = true;

// This is where I change the state of the model whose button was clicked. But the change is not reflected in the UI Proposals.Where(prop => prop.Id == proposal.Id).FirstOrDefault().State = ProposalStates.Approved;

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
            throw;
        }
        finally
        {
            IsBusy = false;
        }
    }

Your DataTrigger setters are having no effect because you've already set local values on the button which take precedence over triggers.

You need to set default values in a style instead

<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. Did you mean "Text" or "Content" for the button text?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM