我在窗口上有一个复选框控件。我想要执行一个将调用相关视图模型中的方法的命令。我还需要复选框的值。我似乎无法找到将命令与复选框相关联的方法。有没有人做过这个?在Checkbox上执行命令。检查或取消选中
回答
<CheckBox Content="CheckBox"
Command="{Binding YourCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />
...并将CommandParameter绑定到CheckBox的值,使用RelativeSource.Self – 2011-04-06 12:28:57
...并将CommandParameter绑定到CheckBox的值,使用CommandParameter = {{Binding IsChecked,RelativeSource = {RelativeSource Self},Mode = OneWay}' – Wally 2013-09-05 20:15:38
I假定该命令必须实现ICommand?它为我工作(我没有尝试任何其他)。 – 2015-06-06 03:14:38
这工作,你需要什么 -
<CheckBox CommandParameter="{Binding}"
Command="{Binding DataContext.AddRemovePresetAssignmentCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
Content="{Binding Path=Name}">
如果你使用MVVM,你可以使用事件触发这样的:
<CheckBox IsChecked="{Binding ServiceOrderItemTask.IsCompleted, Mode=TwoWay}" Content="{Binding ServiceOption.Name}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding DataContext.IsCompletedCheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type t:RadGridView}}}" CommandParameter="{Binding}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding DataContext.IsCompletedUncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type t:RadGridView}}}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
优秀,但在为了使用它,您需要在您的XAML中使用这些命名空间: 'xmlns:t =“http://schemas.telerik.com/2008/xaml/presentation”''xmlns:i =“http://schemas.microsoft。com/expression/2010/interactivity“' – 2015-12-28 07:52:42
以及在视图模型中如何实现? – gts13 2018-01-09 11:29:07
当你只需要CheckBox的状态(选中或未选中),则不需要参数。
CheckBox box = e.OriginalSource as CheckBox;
if(box.IsChecked.Value)
DoThis();
else
DoAnotherMethod();
“E”是ExecutedRoutedEventArgs参数的命令:当您使用此代码,可以检测复选框的状态。您需要box.IsChecked.Value,因为box.IsChecked来自Type bool ?.
我迟到了......我用Rohit Vats回答了这个问题。
该示例是一个工作代码提取,它只是在这里帮助理解每个方面。它是一个图钉,可以是活动的或不活动的,它使用DelegateCommand。您也可以使用RelayCommand或其他类似的类来完成相同的工作。
命令:
using System.Windows.Input;
namespace HQ.Wpf.Util.Command
{
public class StandardCommand
{
public static RoutedUICommand PinPropertyGrid = new RoutedUICommand("Pin property grid", "PinPropertyGrid", typeof(StandardCommand));
的XAML:
<CheckBox HorizontalAlignment="Right"
VerticalAlignment="Top"
Margin="2,0,3,0"
Command="{Binding CommandPinPropertyGrid}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}">
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid>
<Image x:Name="ImagePushpin" Width="16" Height="16" Source="pack://application:,,,/WpfUtil;component/Images/PushpinUnpinned16x16.png" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="ImagePushpin" Property="Source" Value="pack://application:,,,/WpfUtil;component/Images/PushpinPinned16x16.png" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
型号:
public MainWindowViewModel()
{
CommandPinPropertyGrid = new DelegateCommand<bool>(PinPropertyGrid);
...
// ******************************************************************
public DelegateCommand<bool> CommandPinPropertyGrid { get; private set; }
public void PinPropertyGrid(bool pinned)
{
this.IsPropertyGridPinned = pinned;
}
DelegateCommand:
using System;
using System.Windows.Input;
namespace HQ.Wpf.Util.Command
{
/// <summary>
/// Represents a command that forwards the <c>Execute</c> and <c>CanExecute</c> calls to specified delegates.
/// </summary>
public class DelegateCommand<T> : ICommand
{
private readonly Action<T> _executeCallback;
private readonly Predicate<T> _canExecuteCallback;
/////////////////////////////////////////////////////////////////////////////////////////////////////
// OBJECT
/////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand<T>"/> class.
/// </summary>
/// <param name="executeCallback">The execute callback delegate.</param>
public DelegateCommand(Action<T> executeCallback)
: this(executeCallback, null)
{
// No-op
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand<T>"/> class.
/// </summary>
/// <param name="executeCallback">The execute callback delegate.</param>
/// <param name="canExecuteCallback">The can execute callback delegate.</param>
public DelegateCommand(Action<T> executeCallback, Predicate<T> canExecuteCallback)
{
if (executeCallback == null)
throw new ArgumentNullException("executeCallback");
this._executeCallback = executeCallback;
this._canExecuteCallback = canExecuteCallback;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERFACE IMPLEMENTATION
/////////////////////////////////////////////////////////////////////////////////////////////////////
#region ICommand Members
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null"/>.</param>
/// <returns>
/// <c>true</c> if this command can be executed; otherwise, <c>false</c>.
/// </returns>
public bool CanExecute(object parameter)
{
return (this._canExecuteCallback == null) ? true : this._canExecuteCallback((T)parameter);
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (this._canExecuteCallback != null)
CommandManager.RequerySuggested += value;
}
remove
{
if (this._canExecuteCallback != null)
CommandManager.RequerySuggested -= value;
}
}
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null"/>.</param>
public void Execute(object parameter)
{
this._executeCallback((T)parameter);
}
#endregion // ICommand Members
}
}
- 1. 反应检查和取消选中CheckBox
- 2. 如何检查和取消选中CheckBox?
- 3. 复选框命令,检查 - 确定,取消选中不好,我如何绑定取消选中命令?
- 4. Android - 如何检查一个CheckBox取消选中另一个?
- 5. 如何获取linux命令执行结果或消息
- 6. 如何在git中获取或拉取命令后立即执行命令?
- 7. 如何在读取日志以检查命令在执行另一个命令之前是否完成执行?
- 8. 取消选中一个CheckBox取消选中三个?
- 9. 如何在DataGrid中的多个选定行上执行命令?
- 10. 在HTA中获取并执行命令
- 11. 通过Python中的os.system在命令行上执行MySQL查询
- 12. jQuery - 检查所有/取消选中所有复选框 - 然后取消选中(或检查)其他人
- 13. 如何在执行vba之前检查命令是否可行?
- 14. 执行读取命令
- 15. SQL选择命令在执行时消耗100%的CPU
- 16. 在C#上执行PowerShell命令行
- 17. 在Linux中执行命令
- 18. 在ubuntu中执行命令
- 19. 在bash命令中执行
- 20. 在Node.js中执行命令
- 21. 在javascript中执行命令
- 22. 更改CheckBox的颜色或取消选中?
- 23. 检查在UITableViewCell中取消选择
- 24. 当在itemRenderer中滚动我的checkBox被选中/取消选中
- 25. 如何获取在命令行执行的命令?
- 26. 在pyscopg2中取消查询执行
- 27. 检查上一个命令
- 28. 执行取消命令编辑被压在GridView控件
- 29. 消除从命令行执行的sqlplus查询中的不需要的消息
- 30. 在shell_exec命令中运行后检查tar命令的状态?
只是看看这个链接http://stackoverflow.com/questions/959431/how-do-i-attach-commands-to-the-checking-and-unchecking-of-一个复选框 – 2011-04-06 12:15:07