首页 > 解决方案 > 查看 IsEnabled 属性不适用于 Xamarin 窗体

问题描述

这是我的 Listview Inside Listview 按钮 IsEnabled 属性不起作用,IsEnabled False 不起作用。我遵循了此步骤,但仍然无法正常工作 https://forums.xamarin.com/discussion/47857/setting-buttons-isenabled-to-false-does-not-disable-button 在我的 ViewModel 中

OrderItems=PopuldateOrders();// getting List Items

<ListView x:Name="OrderItems" VerticalOptions="Fill" 
                                  BackgroundColor="White" HasUnevenRows="True" 
                                  SeparatorVisibility="None" ItemsSource="{Binding OrderItems}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <ContentView BackgroundColor="White">
                                            <Grid BackgroundColor="Transparent" Margin="0" VerticalOptions="FillAndExpand" x:Name="Item">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="10*"/>
                                                    <ColumnDefinition Width="18*"/>
                                                    <ColumnDefinition Width="18*"/>
                                                    <ColumnDefinition Width="17*"/>
                                                    <ColumnDefinition Width="20*"/>
                                                    <ColumnDefinition Width="17*"/>
                                                </Grid.ColumnDefinitions>
                                                <Label Text="{Binding PullSheetId}" Grid.Row="0" IsVisible="False"/>
                                                <controls:CheckBox Checked="{Binding IsChecked}" Grid.Row="0" Grid.Column="0" IsVisible="{Binding IsEnableShipBtn}" Scale=".8"/>
                                                <Label Text="{Binding KitSKU}" Grid.Row="0" Grid.Column="1" 
                                                   HorizontalTextAlignment="Center" VerticalOptions="Center" FontSize="Small" TextColor="Black"/>
                                                <Label Text="{Binding SKU}" Grid.Row="0" Grid.Column="2" 
                                                   HorizontalTextAlignment="Center" VerticalOptions="Center" FontSize="Small" TextColor="{Binding ItemColor}"/>
                                                <Label Text="{Binding ReqPackQty}" Grid.Row="0" Grid.Column="3" 
                                                   HorizontalTextAlignment="Center" VerticalOptions="Center" FontSize="Small" TextColor="Black"/>
                                                <local:EntryStyle Scale=".6" Text="{Binding ScanQuantity}" Grid.Row="0" Keyboard="Numeric"
                                                                  Grid.Column="4" HorizontalTextAlignment="Center" 
                                                                  VerticalOptions="Center" Placeholder="Qty" IsEnabled="True" x:Name="QtyEntry"
                                                                  >
                                                    <local:EntryStyle.Behaviors>
                                                        <eventToCommand:EventToCommandBehavior EventName="TextChanged"  
                                                                                               Command="{Binding Source={x:Reference OrderItems}, Path=BindingContext.ChangeItemQty}"
                                                                                                CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"
                                                                                               />
                                                    </local:EntryStyle.Behaviors>
                                                </local:EntryStyle>
                                                <Button Text="Ship" Scale=".6" Grid.Row="0" Grid.Column="5"  
                                                    VerticalOptions="Center" BackgroundColor="#6eb43a" TextColor="White" 
                                                    BorderRadius="20" CornerRadius="20" BorderColor="{Binding isError}" BorderWidth="3" 
                                                    MinimumWidthRequest="60"
                                                        x:Name="ShipBtn" 
                                                        Command="{Binding Source={x:Reference OrderItems}, Path=BindingContext.SubmitSingleItem}" 
                                                        IsEnabled="{Binding IsEnableShipBtn}" IsVisible="{Binding IsEnableShipBtn}"
                                                        CommandParameter="{Binding .}" 
                                                        />

                                            </Grid>
                                        </ContentView>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                            <ListView.Behaviors>
                                <eventToCommand:EventToCommandBehavior EventName="ItemTapped" Command="{Binding PackerItemsItemTapped}"/>
                            </ListView.Behaviors>
                        </ListView>

如何解决这个问题?

标签: xamarinxamarin.forms

解决方案


您可以实现CanExecute方法ICommand来替换 Button 的 IsEnabled 属性。你可以参考我的demo。

这是我演示的 GIF。

在此处输入图像描述

首先,您可以看到 MainPage.xaml。为按钮绑定模型视图和设置命令。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:TestDemo"
         x:Class="TestDemo.MainPage">
<!--BindingContext from  ButtonExecuteViewModel -->
<StackLayout>
    <StackLayout.BindingContext>
       <local:ButtonExecuteViewModel/>
    </StackLayout.BindingContext>

    <Button
         Text="click me to enable following button"
         Command="{Binding NewCommand}"/>

    <Button
         Text="Cancel"
         Command="{Binding CancelCommand}"/>

</StackLayout>

这是视图模型 ButtonExecuteViewModel.cs。可以看到 的构造方法ButtonExecuteViewModel,它设置了executeandcanExecute来实现 Button 的 isEnable。

public class ButtonExecuteViewModel : INotifyPropertyChanged
{

    bool isEditing;
    public event PropertyChangedEventHandler PropertyChanged;
    public ButtonExecuteViewModel()
    {

        NewCommand = new Command(
            execute: () =>
            {

                IsEditing = true;
                RefreshCanExecutes();
            },
            canExecute: () =>
            {
                return !IsEditing;
            });



        CancelCommand = new Command(
            execute: () =>
            {
                IsEditing = false;
                RefreshCanExecutes();
            },
            canExecute: () =>
            {
                return IsEditing;
            });

    }
    void RefreshCanExecutes()
    {
        (NewCommand as Command).ChangeCanExecute();

        (CancelCommand as Command).ChangeCanExecute();
    }

    public ICommand NewCommand { private set; get; }

    public ICommand CancelCommand { private set; get; }

    public bool IsEditing
    {
        private set { SetProperty(ref isEditing, value); }
        get { return isEditing; }
    }
    //Determine if it can be executed
    bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (Object.Equals(storage, value))
            return false;

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

推荐阅读