Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Viewmodel绑定到在WPF中使用转换器的CommandParameter_Wpf_Data Binding - Fatal编程技术网

Viewmodel绑定到在WPF中使用转换器的CommandParameter

Viewmodel绑定到在WPF中使用转换器的CommandParameter,wpf,data-binding,Wpf,Data Binding,我正在尝试将一个数字绑定到视图模型中的枚举。我无法通过转换器将值从视图传递到viewmodel。这可能吗?我还没有在网上看到任何解决这个问题的方法,我的尝试也都没有成功 视图模型 public enum TimerOptions { FifteenMinutes, OneHour, Tomorrow } private ICommand _timerCommand; public ICommand TimerCommand => _timerCommand ??

我正在尝试将一个数字绑定到视图模型中的枚举。我无法通过转换器将值从视图传递到viewmodel。这可能吗?我还没有在网上看到任何解决这个问题的方法,我的尝试也都没有成功

视图模型

public enum TimerOptions
{
    FifteenMinutes,
    OneHour, 
    Tomorrow
}

private ICommand _timerCommand;
public ICommand TimerCommand => _timerCommand ?? 
                           (_timerCommand = new RelayCommand<TimerOptions>(StartTimer));

private async void StartTimer(TimerOptions option){ .... }
公共枚举时间选项
{
十五分钟,
一小时,
明天
}
专用ICommand_timer命令;
公共ICommand TimerCommand=>\u TimerCommand??
(_timerCommand=新的RelayCommand(StartTimer));
专用异步void StartTimer(TimerOptions选项){..}
查看

<Button Command="{Binding TimerCommand}" 
        Tag="0" 
        CommandParameter="{Binding Path=Tag, Converter={StaticResource BidirectionalEnumConverter}}">15 minutes</Button>
15分钟
在上面的示例中,当用户单击按钮时,我希望我的函数获取第一个枚举值
fifteennminutes
,作为参数。我尝试过添加一个
标签
(如图所示),在按钮上添加一个
x:Name
,并在绑定中使用
源代码和
路径。什么都没起作用

这可能吗?我已经试着解决这个问题好几个小时了,我在网上什么也没找到

我希望我的函数获取fifteennminutes的第一个枚举值作为参数

为什么不从命令参数中的字符串上传入的信息在VM中执行转换器操作呢

示例

<Button Command="{Binding TimerCommand}" 
        Tag="1" 
        CommandParameter=1
}


这是我使用的指挥结构,而不是中继

public class OperationCommand : ICommand
{

    #region Variables

    private Func<object, bool> CanExecuteHandler { get; set; }
    private Action<object> ExecuteActionHandler { get; set; }

    public bool InSeparateThread { get; set; }
    #endregion

    #region Properties

    #endregion

    #region Construction/Initialization

    public OperationCommand(Action<object> executeAction, bool inSeparateThread = false)
    {
        InSeparateThread = inSeparateThread;
        ExecuteActionHandler = executeAction 
                                   ?? throw new ArgumentNullException(nameof(executeAction));
    }

    public OperationCommand(Action<object> executeAction, 
                            Func<object, bool> canExecute) : this(executeAction)
    {
        CanExecuteHandler = canExecute;
    }

    // Here to adhere to ICommand, change to below if needed
    //public event EventHandler CanExecuteChanged;
    event EventHandler ICommand.CanExecuteChanged
    {
        add {}
        remove {}
    }

    #endregion

    #region Methods

    public bool CanExecute(object parameter) => CanExecuteHandler?.Invoke(parameter) ?? true;

//        public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, new EventArgs());

    public void Execute(object parameter)
    {
        ExecuteActionHandler?.Invoke(parameter);
    }

    #endregion
}
}
公共类操作命令:ICommand
{
#区域变量
私有函数CanExecuteHandler{get;set;}
私有操作ExecuteActionHandler{get;set;}
公共布尔不独立读取{get;set;}
#端区
#区域属性
#端区
#区域构造/初始化
公共操作命令(Action executeAction,bool inSeparateThread=false)
{
Inseparethread=Inseparethread;
ExecuteActionHandler=executeAction
?抛出新ArgumentNullException(nameof(executeAction));
}
公共操作命令(Action executeAction,
Func canExecute):此(执行)
{
CanExecuteHandler=canExecute;
}
//此处要遵守ICommand,请根据需要更改为以下内容
//公共事件处理程序CanExecuteChanged;
事件事件处理程序ICommand.CanExecuteChanged
{
添加{}
删除{}
}
#端区
#区域方法
public bool CanExecute(对象参数)=>CanExecuteHandler?调用(参数)??真;
//public void raiseCanecuteChanged()=>CanExecuteChanged?.Invoke(这是新的EventArgs());
public void Execute(对象参数)
{
ExecuteActionHandler?.Invoke(参数);
}
#端区
}
}

如何声明双向alenumconverter
?是否达到了它的
Convert()
方法?似乎
Command=“{Binding StartTimer}”
应该是
Command=“{Binding TimerComand}”
。除此之外,为了将枚举值传递给CommandParameter,只需写入
CommandParameter=“{x:Static local:TimerOptions.fifteennminutes}”
@PavelAnikhouski,我从这个答案中获取了转换器。它的工作原理和我在其他地方使用过的一样。@Clemens对不起,那是个打字错误。我拿着我的代码,试图简化它,并复制了错误的东西。我无法使用静态枚举,因为枚举位于视图模型中的另一个项目中。我不知道如何在xaml中导入它。通过引用库并添加一个xaml命名空间,如
xmlns:lib=“clr namespace:YourLibraryNamespace;assembly=YourLibraryAssembly”
。然后写
lib:TimerOptions.fifteennminutes
public class OperationCommand : ICommand
{

    #region Variables

    private Func<object, bool> CanExecuteHandler { get; set; }
    private Action<object> ExecuteActionHandler { get; set; }

    public bool InSeparateThread { get; set; }
    #endregion

    #region Properties

    #endregion

    #region Construction/Initialization

    public OperationCommand(Action<object> executeAction, bool inSeparateThread = false)
    {
        InSeparateThread = inSeparateThread;
        ExecuteActionHandler = executeAction 
                                   ?? throw new ArgumentNullException(nameof(executeAction));
    }

    public OperationCommand(Action<object> executeAction, 
                            Func<object, bool> canExecute) : this(executeAction)
    {
        CanExecuteHandler = canExecute;
    }

    // Here to adhere to ICommand, change to below if needed
    //public event EventHandler CanExecuteChanged;
    event EventHandler ICommand.CanExecuteChanged
    {
        add {}
        remove {}
    }

    #endregion

    #region Methods

    public bool CanExecute(object parameter) => CanExecuteHandler?.Invoke(parameter) ?? true;

//        public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, new EventArgs());

    public void Execute(object parameter)
    {
        ExecuteActionHandler?.Invoke(parameter);
    }

    #endregion
}
}