add Window.Close() to window style

essamce 621 Reputation points
2020-05-14T08:14:55.337+00:00

hi,
i'm trying to define window style with Window.Hide() and inside, here is my code:

<Style
                    x:Key="Window.Style1"
                    TargetType="{x:Type Window}">
                    <Setter Property="WindowStyle" Value="None" />
                    <!--<Setter Property="AllowsTransparency" Value="True" />-->
                    <Setter Property="Height" Value="300" />
                    <Setter Property="Width" Value="200" />
                    <Setter Property="MaxWidth" Value="200" />
                    <!--<Setter Property="loc" Value="Red" />-->
                    <Setter Property="BorderBrush" Value="#0046E7" />
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="ResizeMode" Value="NoResize" />
                    <Setter Property="Background" Value="#FFE9E9E9" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Window}">
                                <Grid Background="{TemplateBinding Background}">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="25" />
                                    </Grid.RowDefinitions>
                                    <ContentPresenter Grid.Row="0" />
                                    <Button
                                        Name="okBtm"
                                        Grid.Row="1"
                                        Width="50"
                                        HorizontalAlignment="Right"
                                        Content="OK" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

and it's possible to add Window.DragMove() instead of using code behind, i mean applying next code in window style:

 private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                this.DragMove();
        }

i want to close the window when okBtm clicked,
any help will be appreciated
thanks in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,096 Reputation points
    2020-05-26T08:56:29.987+00:00

    Please replace the TargetObject= {Binding ElementName=window}" as TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}". You can close the Windows with this style, but not for Page.

    By the way, you can use add Microsoft.xaml.behaviors.WPf in NuGet to replace the two dlls which I provide above and do little modify like below:

    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"  
    …….
    <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                            <i:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" MethodName="Close"/>
                    </i:EventTrigger>
    </i:Interaction.Triggers>
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-05-18T02:17:21.257+00:00

    Welcome to our Microsoft Q&A platform!

    I will show a simple demo how to set Window.Close() in Style:

    1.Set name of the Window where you plan to use the Style as window

    2.use this Add the following dlls in your reference

    Microsoft.Expression.Interactions.dll

    System.Windows.Interactivity.dll

    3.Add them and use them to call Close method in your dictionary

            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"     
            xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    ......
        <Button Name="okBtm" Grid.Row="1" Width="50" HorizontalAlignment="Right" Content="OK" Background="Blue">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <ei:CallMethodAction TargetObject= {Binding ElementName=window}" MethodName="Close"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    

    Thanks.

    1 person found this answer helpful.