xamarin - 查看 IsEnabled 属性在 Xamarin Forms 上不起作用

标签 xamarin xamarin.forms

这是我的 ListView 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>

如何解决这个问题?

最佳答案

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

这是我的演示的 GIF。

enter image description here

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

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

这是 View 模型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));
    }
}

关于xamarin - 查看 IsEnabled 属性在 Xamarin Forms 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946227/

相关文章:

ios - Xamarin iOS 图像更改过渡

c# - 如何使用 Prism.Forms 框架从普通页面导航到 MasterDetailPage?

c# - Xamarin Studio 中的断点未命中

c# - 在 Xamarin 中将 cookie 放在 WebView 上

c# - 如何在没有导航页面的情况下更改状态栏颜色

android xamarin google MapFragment.Map 为空

user-interface - 在 Xamarin 中创建完整性计(状态显示)

azure - 我想使用适用于我的 Xamarin 应用程序的 xamarin Azure SDK 备份和还原 SQLite 数据库文件

ios - 在他拒绝后请求知道他们的位置

android - Xamarin 形成 android 工具栏项目的位置