在WPF中, 有很多这样的属性,你如果注意了它,使用了它,你的程序就会流畅和简洁很多,你的视野也会开阔很多。。。 就像windows.CommandBinding 这个属性一样。。。
CommandBinding 将命令与实现以及确定该命令状态的 PreviewExecuted/Executed 和 PreviewCanExecute/CanExecute 事件相关联
调用 RoutedCommand 的 Execute 或 CanExecute 方法时,在命令目标上引发 PreviewExecuted/Executed 或 PreviewCanExecute/CanExecute 事件。 如果命令目标具有该命令的 CommandBinding,则调用相应的处理程序。 如果命令目标没有该命令的 CommandBinding,则在元素树中路由事件,直到发现具有 CommandBinding 的元素。
举个例子:如果在一个窗体中 有多个Button 具有打印的功能(当然,它们是打印不同的报表):
xmal代码如下:
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.PrintPreview" Executed="DisplayReport" />
</Window.CommandBindings>
<Button Style="{StaticResource SpecsButton}" Name="btnDiaryMissingCustomer"
Command="ApplicationCommands.PrintPreview"
CommandParameter="TimesheetExpenseAdmin_DiaryMissingCustomer__DiaryNoCustomer.mrt" >
<AccessText>Diary Missing Customer</AccessText>
</Button>
<Button Style="{StaticResource SpecsButton}" Name="btnSubmittedTimeReport"
Command="ApplicationCommands.PrintPreview"
CommandParameter="TimesheetExpenseAdmin_SubmittedTimeReport__SubmittedTimeReport.mrt" >
<AccessText>Submitted Time Report</AccessText>
</Button>
<Button Style="{StaticResource SpecsButton}" Name="btnCityReport"
Command="ApplicationCommands.PrintPreview"
CommandParameter="TimesheetExpenseAdmin_CityReport__CityReport.mrt" >
<AccessText>City Report</AccessText>
</Button>
<Button Style="{StaticResource SpecsButton}" Name="btnTrialFees"
Command="ApplicationCommands.PrintPreview"
CommandParameter="TimesheetExpenseAdmin_TrialFees__TrialFees.mrt" >
<AccessText>Trial Fees</AccessText>
</Button>
注意:每个button 里面的 command值都是ApplicationCommands.PrintPreview 这与CommandBinding 里的command是一致的
后台代码(c#)----
//这个方法是由自己写的------注意 methodName必须与上面CommandBinding 里面 Executed="DisplayReport"的一致
在xmal里的CommandParameter的值会传到下面方法中的ExecutedRoutedEventArgs e(这样打印的就是不同的报表)
private void DisplayReport(object sender, ExecutedRoutedEventArgs e) {
if (tsDataSet != null) {
try {
Stimulsoft.Report.StiReport report = new Stimulsoft.Report.StiReport();
report.RegData("File", tsDataSet);
report.Load(@"reports/" + e.Parameter.ToString());
report.ShowWithWpf();
}
catch (Exception ex) {
MessageBox.Show("Error '" + ex.Message + "' occurred trying to display report named '" + e.Parameter.ToString() + "'");
}
}
else
MessageBox.Show("Please fetch the data before trying to print this report.", "No Data For Report");
}
如果说这个例子有点复杂,那下面这个或许会简单点-----这是一个执行关闭的操作。
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CloseCommand"
Name="RootWindow"
>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"
CanExecute="CanExecuteHandler"
/>
</Window.CommandBindings>
<StackPanel Name="MainStackPanel">
<Button Command="ApplicationCommands.Close"
Content="Close File" />
</StackPanel>
</Window>
c#代码
Executed 处理程序调用一个方法来关闭已打开的文件。 CanExecute 处理程序调用一个方法来确定文件是否处于打开状态。 如果文件处于打开状态,CanExecute 将设置为 true;否则将设置为 false
// Executed event handler.
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
// Calls a method to close the file and release resources.
CloseFile();
}
// CanExecute event handler.
private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
// Call a method to determine if there is a file open.
// If there is a file open, then set CanExecute to true.
if (IsFileOpened())
{
e.CanExecute = true;
}
// if there is not a file open, then set CanExecute to false.
else
{
e.CanExecute = false;
}
}