简体   繁体   中英

Xamarin.Forms ChangeCanExecute not using the right parameters

I have a Xamarin.Forms application running in UWP where I use the DLToolkit.Forms.Controls.FlowListView (from daniel-luberda ) to display some kind of grid view with buttons.

The Mvvm framework used is FreshMvvm.

Here is my gridview in XAML:

<c:FlowListView SeparatorVisibility="None" 
    HasUnevenRows="False" FlowColumnMinWidth="100" 
    FlowItemsSource="{Binding Boards}">
    <c:FlowListView.FlowColumnTemplate>
        <DataTemplate>
            <Button  Text="{Binding Number}" 
                Command="{Binding Path=BindingContext.SelectBoardCommand, Source={x:Reference Name=SalesPage}}" 
                CommandParameter="{Binding .}" />
        </DataTemplate>
    </c:FlowListView.FlowColumnTemplate>
</c:FlowListView>

Here is the result (I simplified the XAML concerning color and size) :

网格视图

The button inside my gridview is binded to a command with a parameter. Each time I click on a button, I would like to disable it.

And my command looks like this:

    private ICommand _selectBoardCommand;

    [DoNotNotify]
    public ICommand SelectBoardCommand => _selectBoardCommand = new Command<BoardModel>(board =>
    {
        board.NotUsed = false;
        ((Command<BoardModel>) _selectBoardCommand).ChangeCanExecute();
    }, board => board != null && board.NotUsed);

Pushing on a button calls the command with the right parameter (the board.Number binded to the text of the command is the one I get in the command). But when calling the "CanChangeExecute" in order to disable the command, I can't pass the selected board as argument.

And in the command, when using

((Command<BoardModel>) _selectBoardCommand).ChangeCanExecute();

it calls the Func

board => board != null && board.NotUsed

with the latest item of the list.

Moreover, I needed to put a "board != null" in the ChangeCanExecute because this Func is called a lot of time with null values.

Any idea?

I see two ways out of your problem:
1) Move SelectBoardCommand inside BoardModel . It won't need a parameter and will operate on each model individually;
2) Bind Enabled property of Button to NotUsed property. In this case, user won't be able to call SelectBoardCommand from UI at all.

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