我可以使用CanExecuteChanged更改"可执行"条件吗?
或者......"用于什么"它的使用?
我们对项目中的各种命令进行了很多CanExecute测试。当我们使用Visual Studio测试或AxoCover时,所有测试均正确通过。
我们尝试在执行“ CanExecute”之前添加一些先前的对象初始化,有时它可以工作(或者我们认为如此)。
testedViewModel.Object.InEditMode = inEditMode;
Run Code Online (Sandbox Code Playgroud)
我有一个测试:
[TestCase(true, true, TestName = "Command_InEditMode_CanExecute")]
[TestCase(false, false, TestName = "Command_NotInEditMode_CannotExecute")]
public void CommandCanExecute(bool inEditMode, bool expectedResult)
{
var testedViewModel =
new Mock<SomeViewModel>(inEditMode)
{
CallBase = true
};
testedViewModel.Setup(x => x.InEditMode).Returns(inEditMode);
Assert.AreEqual(expectedResult, testedViewModel.Object.Command.CanExecute(null));
}
Run Code Online (Sandbox Code Playgroud)
有时(并非总是)当詹金斯进行构建和运行单元测试时,一些可以执行的测试失败并显示以下消息:
MESSAGE:
Expected: True
But was: False
+++++++++++++++++++
STACK TRACE:
at Project.CommandCanExecute(Boolean inEditMode, Boolean expectedResult)
Run Code Online (Sandbox Code Playgroud)
问题在于仅在詹金斯身上才发生,并且它是不确定的。
编辑:
好的,还要考虑一件事。属性InEditMode放置在SomeModelView的基础父类中。
我在示例中为您合并了代码。
public BaseViewModel
{
public virtual bool InEditMode {get; set;}
}
public SomeViewModel : BaseViewModel
{
public SomeViewModel () : base …Run Code Online (Sandbox Code Playgroud) 首先要说的是我在WPF和MVVM模式的最开始.
在尝试一些自定义命令时,我想知道如何使用ICommand接口中的CanExecute Methode.
在我的例子中,我有一个SaveCommand,我只能在对象可以保存时启用.我的保存按钮的XAML代码如下所示:
<Button Content="Save" Command="{Binding SaveCommand, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)
这是我的保存类的代码:
class Save : ICommand
{
public MainWindowViewModel viewModel { get; set; }
public Save(MainWindowViewModel viewModel)
{
this.viewModel = viewModel;
}
public bool CanExecute(object parameter)
{
if (viewModel.IsSaveable == false)
return false;
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
viewModel.Save();
}
}
Run Code Online (Sandbox Code Playgroud)
ViewModel中的save属性如下所示:
public ICommand SaveCommand
{
get
{
saveCommand = new Save(this);
return saveCommand;
}
set
{
saveCommand = value;
}
}
Run Code Online (Sandbox Code Playgroud)
这个结构不起作用.当isSaveable为true时,该按钮不会启用其自身.
我正在使用MVVM-Light,我的继电器命令工作正常,我刚刚读到我应该实现CanExecuteChanged和CanExecute.虽然我无法找到一个好的例子.
有没有人有一个如何实现这些的好例子.
CanExecute在无法执行时需要返回False,但不会只是取消按钮?
我什么时候执行CanExecuteChanged?
任何人都有任何好的例子,什么时候使用每一个,我的代码工作没有,但这篇博文说明我应该实现这些项目.
我有点困惑,因为我说我认为我只是将Enabled属性或东西绑定到ViewModel中的属性,所以我可以禁用按钮或类似的控件?
任何理解上的帮助都会非常感激.
编辑
这就是我现在所拥有的...它正在工作,但按钮不是物理禁用只有命令不运行,因为我返回false.我在构造函数中调用CanExecuteMe来强制运行RaiseCanExecuteChanged ...
这在我的viewmodel的construtor中运行
this.Page2Command = new RelayCommand(() => this.GoToPage2(), () => CanExecuteMe);
CanExecuteMe = false;
Run Code Online (Sandbox Code Playgroud)
这是我的其余代码,我从一个例子中得到了它.
private bool _canIncrement = true;
public bool CanExecuteMe
{
get
{
return _canIncrement;
}
set
{
if (_canIncrement == value)
{
return;
}
_canIncrement = value;
// Update bindings, no broadcast
//RaisePropertyChanged(CanIncrementPropertyName);
Page2Command.RaiseCanExecuteChanged();
}
}
public RelayCommand Page2Command
{
get;
private set;
}
private object GoToPage2() …Run Code Online (Sandbox Code Playgroud) 我正在实现一个带有execute和canExecute部分的RelayCommand.RelayCommand在没有canExecute部分的情况下工作,但是当我添加canExecute部分时,该命令会锁定按钮.只要CanExecute部分为true,RelayCommand仅检查是否可以执行按钮.一旦canExecute部分变为false,就不能再单击该按钮,即使它应该被按下.我如何确保每次点击按钮它控制是否可以执行,并且一旦无法执行它就不会永久锁定它?
RedoCommand = new RelayCommand(undoRedoController.Redo,undoRedoController.CanRedo);
public bool CanRedo()
{
redoStack.Count();
redoStack.Any();
return redoStack.Any();
}
public void Redo()
{
if (redoStack.Count() <= 0) throw new InvalidOperationException();
IUndoRedoCommand command = redoStack.Pop();
undoStack.Push(command);
command.Execute();
}
public class UndoRedoController
{
private static UndoRedoController controller = new UndoRedoController();
private readonly Stack<IUndoRedoCommand> undoStack = new Stack<IUndoRedoCommand>();
private readonly Stack<IUndoRedoCommand> redoStack = new Stack<IUndoRedoCommand>();
private UndoRedoController() { }
public static UndoRedoController GetInstance() { return controller; }
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的按钮绑定到命令
<Button Content="Add" Margin="10,10,10,0" Command="{Binding SaveCommand}" ></Button>
Run Code Online (Sandbox Code Playgroud)
我的命令代码
public ICommand SaveCommand
{
get;
internal set;
}
private bool CanExecuteSaveCommand()
{
return DateTime.Now.Second % 2 == 0;
}
private void CreateSaveCommand()
{
SaveCommand = new DelegateCommand(param => this.SaveExecute(), param => CanExecuteSaveCommand());
}
public void SaveExecute()
{
PharmacyItem newItem = new PharmacyItem();
newItem.Name = ItemToAdd.Name;
newItem.IsleNumber = ItemToAdd.IsleNumber;
newItem.ExpDate = ItemToAdd.ExpDate;
PI.Add(newItem);
}
Run Code Online (Sandbox Code Playgroud)
代码有效地阻止命令运行基于CanExecuteSaveCommand但是按钮永远不会被禁用,有没有办法实现这一点?
我在Eclipse Luna RCP中遇到了命令处理程序的问题.
在我的E4应用程序模型中,我定义了一些必须在某些情况下才能启用的命令和相关处理程序.出于这个原因,在我的处理程序POJO中,我实现了注释用于@CanExecute检查所需条件的方法.
我还定义了与这些命令相关的菜单和工具栏项.
问题是我的@CanExecute方法没有被正确调用,因此,菜单和工具栏项不会相应地启用/禁用.
特别是,对于菜单项,@CanExecute方法仅在应用程序启动时调用几次,但在此之后从不调用.
相反,对于工具栏项,@CanExecute仅在活动上下文更改时(例如,更改活动部件或打开新shell时)调用方法.
在开普勒,行为完全不同(并按预期工作):
@CanExecute每次显示菜单时都会调用这些方法@CanExecute每400ms 调用一次方法这是Luna中的已知错误吗?您知道任何可能的解决方法来实现预期的行为吗?
谢谢!
我的wpf-mvvm应用程序中有一个按钮控件.
我使用ICommand属性(在viewmodel中定义)将按钮单击事件绑定到viewmodel.
我有 - >我的ICommand实现的执行和canexecute参数(RelayCommand).
即使CanExecute为false ...按钮未被禁用... WHEN按钮CONTENT为IMAGE
但是,当按钮内容是text..enable/disable工作正常.
<Button DockPanel.Dock="Top"
Command="{Binding Path=MoveUpCommand}">
<Button.Content>
<Image Source="/Resources/MoveUpArrow.png"></Image>
</Button.Content>
<Style>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value=".5" />
</Trigger>
</Style.Triggers>
</Style>
</Button>
Run Code Online (Sandbox Code Playgroud) 我的xaml中有几个Tiles(TileLayoutControl类)(本例中只显示了2个),其可见性绑定到布尔属性并通过BooleanToVisibilityConverter转换.
这很好用.我的问题是
我可以将可见性绑定到Command,以便我可以删除那些几个布尔属性的需要吗?
类似于将Visibility绑定到Command.CanExecute的东西
如果是,我该如何实现?任何帮助将非常感谢!谢谢.
<dxlc:Tile Command="{Binding Tile1Command}"
Visibility="{Binding Path=IsTile1Visible , Converter={StaticResource BooleanToVisibilityConverter}}"/>
<dxlc:Tile Command="{Binding Tile2Command}"
Visibility="{Binding Path=IsTile2Visible , Converter={StaticResource BooleanToVisibilityConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
视图模型
private bool _isTile1Visible;
public bool IsTile1Visible
{
get { return _isTile1Visible; }
set { this.RaiseAndSetIfChanged(ref _isTile1Visible, value); }
}
public ReactiveCommand Tile1Command { get; private set; }
Tile1Command = new ReactiveCommand();
Tile1Command.Subscribe(p => PerformTile1Operation());
Run Code Online (Sandbox Code Playgroud) 我有一个表单,其中包含一些绑定到某个对象属性的 TextBox:
<Label Content="Car Id:"/>
<TextBox Text="{Binding Path=Car.CarId, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
我有一个“确定按钮”映射到像这样的 CanExecute() 命令(不显示事件部分,但确保它有效):
private bool CanExecute()
{
return _vm.Model.Car.CarId !=0;
}
Run Code Online (Sandbox Code Playgroud)
问题:
预先感谢您对此的提示!
canexecute ×10
wpf ×8
mvvm ×5
c# ×3
mvvm-light ×2
relaycommand ×2
e4 ×1
eclipse-luna ×1
eclipse-rcp ×1
handler ×1
icommand ×1
jenkins ×1
moq ×1
reactiveui ×1
visibility ×1
xaml ×1