开发者

Where to set window property in XAML

I found some code for an attached property that will let me escape close a window called as a dialog here on SO.

But I can't figure out where to set the property.

This might be complicated by the fact that this is a RibbonWindow and not a normal Window but I don't think so.

The attached property will be ab:WindowService.EscapeClosesWind开发者_运维百科ow="True". Where in the xaml should it go??

Cheers,

Berryl

ERROR

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) 

The gist of the attached property is

    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();
    }

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


I created a sample application to recreate that error you mentioned. And I failed to reproduce that error. It easily compiled in my app.

Can you check with a sample application and try to find any differences?

Recreation:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜