首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在XAML中设置窗口属性的位置

在XAML中设置窗口属性的位置
EN

Stack Overflow用户
提问于 2010-12-19 02:44:03
回答 1查看 2K关注 0票数 1

我找到了一些附加属性的代码,它可以让我退出关闭一个名为对话框here on SO的窗口。

但我想不出该在哪里设置属性。

这可能会因为这是一个RibbonWindow而不是一个普通窗口的事实而变得复杂,但我不这么认为。

附加的属性将为ab:WindowService.EscapeClosesWindow="True“。在xaml中它应该放在哪里??

干杯,

Berryl

错误

代码语言:javascript
复制
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) 

附加属性的要点是

代码语言:javascript
复制
    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();
    }

窗口xaml

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-19 03:19:55

我创建了一个示例应用程序来重现您提到的那个错误。我没能重现那个错误。它很容易在我的应用程序中编译。

您是否可以检查示例应用程序,并尝试找出任何差异?

娱乐:

代码语言:javascript
复制
public static class WindowService
{
    public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
       "EscapeClosesWindow",
       typeof(bool),
       typeof(WindowService),
       new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));

    public static bool GetEscapeClosesWindow(DependencyObject d)
    {
        return (bool)d.GetValue(EscapeClosesWindowProperty);
    }

    public static void SetEscapeClosesWindow(DependencyObject d, bool value)
    {
        d.SetValue(EscapeClosesWindowProperty, value);
    }

    private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Window target = (Window)d;
        if (target != null)
        {
            target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
        }
    }

    private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        Window target = (Window)sender;

        if (e.Key == Key.Escape)
            target.Close();
    }
}

XAML:

代码语言:javascript
复制
<ribbon:RibbonWindow x:Class="WpfApplication1.RibbonWindow1"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
                     xmlns:local="clr-namespace:WpfApplication1"
                     local:WindowService.EscapeClosesWindow="True"
                     Title="RibbonWindow1"
                     x:Name="Window"
                     Width="640"
                     Height="480">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ribbon:Ribbon x:Name="Ribbon">
            <ribbon:RibbonTab x:Name="HomeTab"
                              Header="Home">
                <ribbon:RibbonGroup x:Name="Group1"
                                    Header="Group1"></ribbon:RibbonGroup>
            </ribbon:RibbonTab>
        </ribbon:Ribbon>
    </Grid>
</ribbon:RibbonWindow>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4479485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档