[UWP / MVVM] RadDataGrid数据模板列中的启用/禁用按钮,这些按钮根据条件绑定有命令 [英] [UWP/MVVM]Enable/Disable Button in RadDataGrid Data Template Column that have commands bound to them upon conditions
问题描述
我已经设置了bool属性并将其绑定到xaml中的IsEnabled,但是ICommand CanExecute方法覆盖了xaml中的IsEnabled,因此我的bool属性无效。
当我在视图模型的CanExecute方法中定义条件时,它要么禁用该方法绑定到的所有按钮,要么启用所有按钮。 p>
它是一个网格,每行显示3个不同的按钮,每个按钮都转到一个新的xaml屏幕。如果该行上没有满足特定条件的数据,则该按钮处于禁用状态。
我该如何进行设置,以使按钮处于
自定义命令:
公共类CustomCommand :ICommand
{
private Action< object>执行;
private Predicate< object> canExecute;
public CustomCommand(Action< object> execute,Predicate< object> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
公共事件EventHandler CanExecuteChanged
{
添加
{
}
删除
{
}
}
public bool CanExecute(object parameter)
{
//抛出新的NotImplementedException() ;
bool b = canExecute == null吗? true:canExecute(参数);
返回b;
}
public void Execute(对象参数)
{
execute(parameter);
}
}
xaml
< DataTemplate>
< Button Command = {Binding Source = {StaticResource VM},
Path = Command} CommandParameter = {Binding}>
< SymbolIcon Symbol = Edit Foreground = AliceBlue />
< / Button>
< / DataTemplate>
可以在VM中执行
私人布尔CanGetDetails(object obj)
{
返回true;
}
您总是可以有条件的您的自定义命令的CanExecute函数中的语句,不需要将IsEnabled属性与绑定到命令的按钮绑定。这是一个示例实现,希望对您有所帮助。
自定义命令:
公共类CustomCommand< T> :ICommand
{
private readonly Action< T> _行动;
私有只读谓词< 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(对象参数)
{
_action((T)parameter);
}
公共事件EventHandler CanExecuteChanged;
}
在这里您可以看到,我创建了一个实现ICommand接口的对象,此自定义命令接受用于评估条件的通用类型参数(CanExecute:此参数指示是启用还是禁用命令(在UI中为按钮),通常用于检查权限以及其他某些条件),该参数为也用于执行动作(执行:要执行的实际逻辑/动作)。命令构造器接受包含这两种方法签名的委托参数,调用者可以选择lambda或标准方法来填充这些参数。
示例ViewModel:
公共类ViewModel1:INotifyPropertyChanged
{
public ViewModel1()
{
//测试数据。
Items = new ObservableCollection< ItemViewModel>
{
新ItemViewModel {代码= 001,描述=油漆},
新ItemViewModel {代码= 002,描述=画笔},
新ItemViewModel {代码= 003,说明=}
};
EditCommand = new CustomCommand< ItemViewModel>(Edit,CanEdit);
}
public CustomCommand< ItemViewModel> EditCommand {get; }
私人布尔CanEdit(ItemViewModel item)
{
返回项目?说明!= string.Empty;
}
private void Edit(ItemViewModel item)
{
Debug.WriteLine( Selected Item:{0}-{1},item.Code,商品描述);
}
私人ObservableCollection< ItemViewModel> _item {get;组; }
public ObservableCollection< ItemViewModel>项目
{
get => _items;
set
{
_items =值;
OnPropertyChanged();
}
}
公共事件PropertyChangedEventHandler PropertyChanged;
受保护的虚拟无效OnPropertyChanged([CallerMemberName]字符串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文本= {绑定代码} />
< TextBlock文字= {绑定描述} />
< Button Content = Edit Command = {Binding DataContext.EditCommand,ElementName = root} CommandParameter = {Binding} />
< / StackPanel>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ItemsControl>
< / Grid>
< / Page>
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.
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.
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?
Custom Command:
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);
}
}
xaml
<DataTemplate>
<Button Command="{Binding Source={StaticResource VM},
Path=Command}" CommandParameter="{Binding}" >
<SymbolIcon Symbol="Edit" Foreground="AliceBlue" />
</Button>
</DataTemplate>
CanExecute in VM
private bool CanGetDetails(object obj)
{
return true;
}
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.
Custom Command:
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;
}
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.
Sample ViewModel:
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屋!