View IsEnabled属性在Xamarin表单上不起作用

时间:2018-12-27 13:54:57

标签: xamarin xamarin.forms

这是我的列表视图  内部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>

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以在CanExecute中实现ICommand方法来替换Button的IsEnabled属性。您可以参考我的演示。

这是我的演示的GIF。

enter image description here

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

<?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的构造方法,该方法将executecanExecute设置为实现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));
    }
}