question

essamce avatar image
0 Votes"
essamce asked ·

add Window.Close() to window style

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
1 Vote"
AlexLi-MSFT answered ·

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>


· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi @AlexLi-MSFT thanks for replying , does Microsoft.xaml.behaviors.WPf as a NuGet needs to be distributed with my project ?

0 Votes 0 ·

This should be not necessary, if you install the Microsoft.xaml.behaviors.Wpf in the NuGet (Step: Right click the project>Choose Manage NuGet package> search and install ), the Microsoft.Xaml.Behaviors.dll and App.exe will be generated after you build/run in the same folder. Clicking on and running App.exe is the same as running VS.

1 Vote 1 ·
AlexLi-MSFT avatar image
1 Vote"
AlexLi-MSFT answered ·

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 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi @AlexLi-MSFT
thanks for replying,
i want my style to be abstracted and know nothing about the window that uses it, ie i want my style to be in a resource dictionary and each window uses this style has a different name.

0 Votes 0 ·