我找到了一些附加属性的代码,它可以让我在 SO 上关闭一个称为对话框的窗口。
但我不知道在哪里设置属性。
这可能会因为这是一个 RibbonWindow 而不是一个普通的 Window 而变得复杂,但我不这么认为。
附加属性将是 ab:WindowService.EscapeCloseWindow="True"。它应该在 xaml 中的哪个位置?
干杯,
贝里尔
错误
Unable to cast object of type
'Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance' to type
'Microsoft.Windows.Controls.Ribbon.RibbonWindow'.
at AttachedBehaviors.WindowService.OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
附加财产的要点是
private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var target = (Window) d;
if (target != null)
target.PreviewKeyDown += Window_PreviewKeyDown;
}
/// <summary>Handle the PreviewKeyDown event on the window</summary>
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
var target = (Window) sender;
// If this is the escape key, close the window
if (e.Key == Key.Escape)
target.Close();
}
窗口xml
<r:RibbonWindow x:Class="SCA.Presentation.Wpf.Views.TimeSheet.WeeklyTimeSheetView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:SCA.Presentation.Wpf.Views.TimeSheet"
xmlns:ab="clr-namespace:SCA.Presentation.Wpf.AttachedBehaviors;assembly=Smack.Core.Presentation.Wpf"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Background="{DynamicResource WaveWindowBackground}"
FontFamily="Arial" FontSize="12"
Width="900" Height="600"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
ab:WindowService.EscapeClosesWindow="True" <=== ERROR
>
<r:RibbonWindow.Resources>
<ResourceDictionary Source="TimesheetResources.xaml" />
</r:RibbonWindow.Resources>