积极答复者
有关EventCommand的问题

问题
-
在windows8 中 如何实现 wpf 里 EventToCommand 类似的功能( MvvmLight)
因为有限控件内事件无法通过command 进行绑定,对实现mvvm 设计模式有障碍,
希望能得到满意的答案
<i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <cmd:EventToCommand Command="{Binding SelectedShopChangeCommand}" PassEventArgsToCommand="True" /> </i:EventTrigger> </i:Interaction.Triggers>
非常荣幸成为.net 开发者
答案
-
我已经解决这个问题了。
public void ExecuteCommand(object sender, object eventhandler)
{
var parsingok = false;
// The typed parameter
object typedparameter = null;// Set the DataContext to the element DataContext (Otherwise the binding returns NULL
DataContext = Element.DataContext;
//// Get the command
//var command = Command;//Command is null, so don't do anything
if (Command == null)
return;
if (CommandParameter == null) CommandParameter = eventhandler;// Get the parameter
var parameter = CommandParameter;
if (parameter != null)用的以上代码。
稍后整理成demo上传
可以通过command 传eventargs 以及 parameter
非常荣幸成为.net 开发者
- 已标记为答案 GhostZephyrus 2012年9月9日 14:04
-
- 已标记为答案 GhostZephyrus 2012年9月10日 7:22
-
看这个帖子,我实现了:
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
你确定能传事件参数吗? 比如PointerEventArgs 。
你这个例子我之前试过
我现在也实现了eventtocommands
但是 无法将eventArgs 当作 commandParameters 传输
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged"> <cmd:EventToCommand Command="{Binding SelectedShopChangeCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>当我在wpf 里 加上 PassEventArgsToCommand="True"
这个属性之后 就能将该事件的eventArgs 传到command中 。。
非常荣幸成为.net 开发者
传递参数只需要你在代码执行时候封装进入CommandParameter就可以了,我的代码中
private void OnEventRaised(object sender, object e) // the second parameter in Windows.UI.Xaml.EventHandler is Object { ICommand command = (ICommand)(sender as DependencyObject).GetValue(AttachedCommand.CommandProperty); object commandParameter = (sender as DependencyObject).GetValue(AttachedCommand.CommandParameterProperty); if (commandParameter == null) commandParameter = new object[] { sender, e }; if (command != null && command.CanExecute(commandParameter)) { command.Execute(commandParameter); } }
我的commandParameter 其实封装了你的Event Sender 和 Event Args 在里面,然后传递给了Command 执行。Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 GhostZephyrus 2012年9月10日 7:22
全部回复
-
这么做不是把mvvm毁了么。。。。 ..为什么caliburn 支持这种实现方式呢 - -
非常荣幸成为.net 开发者
- 已编辑 GhostZephyrus 2012年9月7日 3:39
-
- 已建议为答案 Jie BaoModerator 2012年9月7日 6:02
- 取消建议作为答案 GhostZephyrus 2012年9月8日 3:44
- 已标记为答案 GhostZephyrus 2012年9月9日 11:52
- 取消答案标记 GhostZephyrus 2012年9月9日 14:02
-
你确定能传事件参数吗? 比如PointerEventArgs 。
你这个例子我之前试过
我现在也实现了eventtocommands
但是 无法将eventArgs 当作 commandParameters 传输
非常荣幸成为.net 开发者
- 已编辑 GhostZephyrus 2012年9月8日 3:45
-
看这个帖子,我实现了:
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
你确定能传事件参数吗? 比如PointerEventArgs 。
你这个例子我之前试过
我现在也实现了eventtocommands
但是 无法将eventArgs 当作 commandParameters 传输
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged"> <cmd:EventToCommand Command="{Binding SelectedShopChangeCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>当我在wpf 里 加上 PassEventArgsToCommand="True"
这个属性之后 就能将该事件的eventArgs 传到command中 。。
非常荣幸成为.net 开发者
-
我已经解决这个问题了。
public void ExecuteCommand(object sender, object eventhandler)
{
var parsingok = false;
// The typed parameter
object typedparameter = null;// Set the DataContext to the element DataContext (Otherwise the binding returns NULL
DataContext = Element.DataContext;
//// Get the command
//var command = Command;//Command is null, so don't do anything
if (Command == null)
return;
if (CommandParameter == null) CommandParameter = eventhandler;// Get the parameter
var parameter = CommandParameter;
if (parameter != null)用的以上代码。
稍后整理成demo上传
可以通过command 传eventargs 以及 parameter
非常荣幸成为.net 开发者
- 已标记为答案 GhostZephyrus 2012年9月9日 14:04
-
- 已标记为答案 GhostZephyrus 2012年9月10日 7:22
-
看这个帖子,我实现了:
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
你确定能传事件参数吗? 比如PointerEventArgs 。
你这个例子我之前试过
我现在也实现了eventtocommands
但是 无法将eventArgs 当作 commandParameters 传输
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged"> <cmd:EventToCommand Command="{Binding SelectedShopChangeCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>当我在wpf 里 加上 PassEventArgsToCommand="True"
这个属性之后 就能将该事件的eventArgs 传到command中 。。
非常荣幸成为.net 开发者
传递参数只需要你在代码执行时候封装进入CommandParameter就可以了,我的代码中
private void OnEventRaised(object sender, object e) // the second parameter in Windows.UI.Xaml.EventHandler is Object { ICommand command = (ICommand)(sender as DependencyObject).GetValue(AttachedCommand.CommandProperty); object commandParameter = (sender as DependencyObject).GetValue(AttachedCommand.CommandParameterProperty); if (commandParameter == null) commandParameter = new object[] { sender, e }; if (command != null && command.CanExecute(commandParameter)) { command.Execute(commandParameter); } }
我的commandParameter 其实封装了你的Event Sender 和 Event Args 在里面,然后传递给了Command 执行。Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 GhostZephyrus 2012年9月10日 7:22