如何在C#WPF应用程序中访问命令绑定? [英] How to access commandbindings in c# WPF application?

查看:71
本文介绍了如何在C#WPF应用程序中访问命令绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#WPF应用程序显示Crystal报表,并且我想在单击Crystal Report Viewer刷新按钮时调用一个函数.

因此,在查看器属性中,我将命令绑定设置为刷新,并且应用程序XAML如下所示

I''m using C# WPF application to display crystal reports and i want to call a function when crystal report viewer refresh button is clicked.

so in viewer properties i set command binding to refresh and application XAML looks like below

<Grid>
    <cr:CrystalReportsViewer Name="reportViewer">
        <cr:CrystalReportsViewer.CommandBindings>
            <CommandBinding Command="NavigationCommands.Refresh"/>
        </cr:CrystalReportsViewer.CommandBindings>
    </cr:CrystalReportsViewer>
</Grid>



知道如何捕获此刷新事件并调用命令处理程序吗?

关于



any idea how to capture this refresh event and call the command handler?

Regards

推荐答案

基本上,不仅需要绑定命令,而且还需要关联事件.例如
Basically it''s not only you need to bind the command but you need to associate events as well. For example
  <commandbinding command="ApplicationCommands.Close">
                  Executed="CloseCommandHandler"
                  CanExecute="CanExecuteHandler"/>
</commandbinding>


// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
    ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);

// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);

// Executed event handler.
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    // Calls a method to close the file and release resources.
    // Do something
}
// CanExecute event handler.
private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{   // If something is true then
    if (true)
        e.CanExecute = true;
    // If not, avoid execute
    else
        e.CanExecute = false;
}



有关如何阅读以下MSDN文章中列出的示例的更多信息. 命令概述 [



For more how about reading the examples listed in the following MSDN article. Commanding Overview[^]


这篇关于如何在C#WPF应用程序中访问命令绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆