【问题标题】:Where to set window property in XAML在 XAML 中设置窗口属性的位置
【发布时间】:2010-12-18 18:44:02
【问题描述】:

我找到了一些附加属性的代码,它可以让我关闭一个称为对话框here on 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();
    }

窗口 xaml

<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>

【问题讨论】:

    标签: wpf windows xaml attached-properties


    【解决方案1】:

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

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

    娱乐:

    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:

    <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>
    

    【讨论】:

    • 这很奇怪 - 只是将属性移动为第一个没有错误,尽管仍然存在抱怨。脆弱,就像 VS 设计师有时那样,但它确实有效。这里的不同之处在于我拥有的 DynamicResource 。干杯
    猜你喜欢
    • 2018-04-11
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多