WPF Window 自定义事件创建并绑定到 ICommand
WPF Window custom event creating and binding to ICommand
这是WPF/MVVM申请。 MainWindow.xaml.cs 后面的代码中有一些代码应该生成自定义事件,并且需要报告此事件的事实(可能带有参数)以查看模型 class (MainWindowViewModel.cs).
例如。我在 partial class MainWindow 中声明了 RoutedEvent TimerEvent 但我无法绑定到视图模型命令,因为此事件在 xaml 代码上不可用。错误:无法识别或无法访问计时器。
如何解决这个问题?谢谢!
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var timer = new Timer();
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Interval = 5000;
timer.Enabled = true;
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
RaiseTimerEvent();
}
// Create a custom routed event
public static readonly RoutedEvent TimerEvent = EventManager.RegisterRoutedEvent(
"Timer", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MainWindow));
// Provide CLR accessors for the event
public event RoutedEventHandler Timer
{
add => AddHandler(TimerEvent, value);
remove => RemoveHandler(TimerEvent, value);
}
void RaiseTimerEvent()
{
var newEventArgs = new RoutedEventArgs(MainWindow.TimerEvent);
RaiseEvent(newEventArgs);
}
}
<Window x:Class="CustomWindowEvent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomWindowEvent"
Title="MainWindow" Height="250" Width="400"
Timer="{Binding TimerCommand}"> // THIS PRODUCE ERROR Timer is not recognized or is not accessible.
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<StackPanel>
<TextBlock Text="{Binding Title}"/>
<Button Width="75"
Height="24"
Content="Run"
Command="{Binding RunCommand}"/>
</StackPanel>
</Grid>
</Window>
您不能像这样将 ICommand
属性 绑定到事件。
当您的 window 发出命令时,您可能会以编程方式调用命令:
void RaiseTimerEvent()
{
var newEventArgs = new RoutedEventArgs(MainWindow.TimerEvent);
RaiseEvent(newEventArgs);
var vm = this.DataContext as MainWindowViewModel;
if (vm != null)
vm.TimerCommand.Execute(null);
}
另一种选择是使用 Microsoft.Xaml.Behaviors.Wpf 包中的 EventTrigger
和 InvokeCommandAction
来调用使用 XAML:
的命令
<Window .. xmlns:i="http://schemas.microsoft.com/xaml/behaviors">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Timer" >
<i:InvokeCommandAction Command="{Binding TimerCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Window>
更多信息请参考this blog post。
这是WPF/MVVM申请。 MainWindow.xaml.cs 后面的代码中有一些代码应该生成自定义事件,并且需要报告此事件的事实(可能带有参数)以查看模型 class (MainWindowViewModel.cs).
例如。我在 partial class MainWindow 中声明了 RoutedEvent TimerEvent 但我无法绑定到视图模型命令,因为此事件在 xaml 代码上不可用。错误:无法识别或无法访问计时器。
如何解决这个问题?谢谢!
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var timer = new Timer();
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Interval = 5000;
timer.Enabled = true;
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
RaiseTimerEvent();
}
// Create a custom routed event
public static readonly RoutedEvent TimerEvent = EventManager.RegisterRoutedEvent(
"Timer", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MainWindow));
// Provide CLR accessors for the event
public event RoutedEventHandler Timer
{
add => AddHandler(TimerEvent, value);
remove => RemoveHandler(TimerEvent, value);
}
void RaiseTimerEvent()
{
var newEventArgs = new RoutedEventArgs(MainWindow.TimerEvent);
RaiseEvent(newEventArgs);
}
}
<Window x:Class="CustomWindowEvent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomWindowEvent"
Title="MainWindow" Height="250" Width="400"
Timer="{Binding TimerCommand}"> // THIS PRODUCE ERROR Timer is not recognized or is not accessible.
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<StackPanel>
<TextBlock Text="{Binding Title}"/>
<Button Width="75"
Height="24"
Content="Run"
Command="{Binding RunCommand}"/>
</StackPanel>
</Grid>
</Window>
您不能像这样将 ICommand
属性 绑定到事件。
当您的 window 发出命令时,您可能会以编程方式调用命令:
void RaiseTimerEvent()
{
var newEventArgs = new RoutedEventArgs(MainWindow.TimerEvent);
RaiseEvent(newEventArgs);
var vm = this.DataContext as MainWindowViewModel;
if (vm != null)
vm.TimerCommand.Execute(null);
}
另一种选择是使用 Microsoft.Xaml.Behaviors.Wpf 包中的 EventTrigger
和 InvokeCommandAction
来调用使用 XAML:
<Window .. xmlns:i="http://schemas.microsoft.com/xaml/behaviors">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Timer" >
<i:InvokeCommandAction Command="{Binding TimerCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Window>
更多信息请参考this blog post。