[UWP/MVVM]RadDataGrid 数据模板列中的启用/禁用按钮根据条件绑定了命令 [英] [UWP/MVVM]Enable/Disable Button in RadDataGrid Data Template Column that have commands bound to them upon conditions

查看:11
本文介绍了[UWP/MVVM]RadDataGrid 数据模板列中的启用/禁用按钮根据条件绑定了命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个 bool 属性并将其绑定到 xaml 中的 IsEnabled 但 ICommand CanExecute 方法覆盖了 xaml 中的 IsEnabled,因此我的 bool 属性无效.

I have set a bool property and have bound it to the IsEnabled in the xaml but the ICommand CanExecute method overrides the IsEnabled in xaml, so my bool property is ineffective.

当我在视图模型的 CanExecute 方法中定义条件时,它要么禁用该方法绑定到的所有按钮,要么启用所有按钮.

When I define the conditions within the CanExecute method in the view model, It either disables all buttons in which the method is bound to, or enables all of them.

它是一个网格,每行显示 3 个不同的按钮,每个按钮转到一个新的 xaml 屏幕.如果按钮所在行的特定条件没有数据,则需要禁用该按钮.

Its a grid that displays 3 different buttons for each row, and each button goes to a new xaml screen. If there is no data for the particular condition on the row the button is on then the button needs to be disabled.

我该如何设置以便在条件下禁用按钮?

How do i go about setting this so that buttons are disabled upon a condition?

自定义命令:

public class CustomCommand : ICommand
{

    private Action<object> execute;
    private Predicate<object> canExecute;
    public CustomCommand(Action<object> execute, Predicate<object> canExecute)
    {
        this.execute = execute;
        this.canExecute = canExecute;

    }

    public event EventHandler CanExecuteChanged
    {
        add
        {

        }
        remove
        {

        }
    }

    public bool CanExecute(object parameter)
    {
        //throw new NotImplementedException();
        bool b = canExecute == null ? true : canExecute(parameter);
        return b;
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }
}

xml

<DataTemplate>
                                <Button Command="{Binding Source={StaticResource VM},
                                Path=Command}" CommandParameter="{Binding}" >
                                    <SymbolIcon Symbol="Edit" Foreground="AliceBlue" />
                                </Button>
</DataTemplate>

可以在虚拟机中执行

 private bool CanGetDetails(object obj)
    {
        return true;
    }

推荐答案

您始终可以在自定义命令的 CanExecute 函数中执行条件语句,无需将 IsEnabled 属性与绑定到命令的按钮绑定.这是一个示例实现,希望对您有所帮助.

You can always do your conditional statement within the CanExecute function of your custom command, no need for you to bind IsEnabled property with your button that is bound to a command. Here's a sample implementation, hope this helps.

自定义命令:

public class CustomCommand<T> : ICommand
{
    private readonly Action<T> _action;
    private readonly Predicate<T> _canExecute;

    public CustomCommand(Action<T> action, Predicate<T> canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute((T)parameter);
    }

    public void Execute(object parameter)
    {
        _action((T)parameter);
    }

    public event EventHandler CanExecuteChanged;
}

正如你在这里看到的,我创建了一个实现 ICommand 接口的对象,这个自定义命令接受一个用于评估条件的通用类型参数(CanExecute:这告诉是启用还是禁用命令(在 UI 中,按钮),通常用于检查权限,和其他某些条件)这个参数也用于执行动作(执行:实际逻辑/要执行的动作),命令构造器接受包含这两个签名的委托参数方法,调用者可以选择 lambda 或标准方法来填充这些参数.

As you can see here, I created an object that implements the ICommand interface, this custom command accepts a generic type parameter which is used to evaluate a condition (CanExecute: this tells whether to enable or disable a command (in UI, the button), normally use to check for permissions, and other certain conditions) this parameter is also used to execute the action (Execute: the actual logic/action to be performed), The command contructor accepts delegate parameters that contain signatures for these 2 methods, the caller may choose lambda or standard methods to fillup these parameters.

示例视图模型:

public class ViewModel1: INotifyPropertyChanged
    {
        public ViewModel1()
        {
            // Test Data.
            Items = new ObservableCollection<ItemViewModel>
            {
                new ItemViewModel{ Code = "001", Description = "Paint" },
                new ItemViewModel{ Code = "002", Description = "Brush" },
                new ItemViewModel{ Code = "003", Description = "" }
            };

            EditCommand = new CustomCommand<ItemViewModel>(Edit, CanEdit);
        }

        public CustomCommand<ItemViewModel> EditCommand { get; }

        private bool CanEdit(ItemViewModel item)
        {
            return item?.Description != string.Empty;
        }

        private void Edit(ItemViewModel item)
        {
            Debug.WriteLine("Selected Item: {0} - {1}", item.Code, item.Description);
        }

        private ObservableCollection<ItemViewModel> _items { get; set; }

        public ObservableCollection<ItemViewModel> Items
        {
            get => _items;
            set
            {
                _items = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

XAML:

<Page x:Name="root"
    x:Class="App1.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:vms="using:App1.ViewModels"
      mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    d:DesignHeight="450" d:DesignWidth="800">
    <Page.DataContext>
        <vms:ViewModel1 x:Name="Model"/>
    </Page.DataContext>
    <Grid>
        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0 0 0 15">
                        <TextBlock Text="{Binding Code}" />
                        <TextBlock Text="{Binding Description}" />
                        <Button Content="Edit" Command="{Binding DataContext.EditCommand, ElementName=root}" CommandParameter="{Binding}" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Page>

这篇关于[UWP/MVVM]RadDataGrid 数据模板列中的启用/禁用按钮根据条件绑定了命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆