Jump to content
  • Advertisement
Sign in to follow this  

[.net] Template close button

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys!

I'm creating a template in my App.xaml.

I'd like all the windows in my application to
have the same layout, with rounded edges, the
same logo in the top left corner, and a close
button top right.

I've gotten all this in place, however...
now I need some code that tracks the button
click event back to the window where the
close button was clicked, and closes that
particular window.

I've tried tracking the parent objects of the
sender object, but they just refer to the objects
that are in the template.

Thanks guys!
Any help is apprechiated :)

Share this post

Link to post
Share on other sites
Please post the code/xaml that shows where and how you added the Close button. A lot depends on that.

Share this post

Link to post
Share on other sites

Yeah, the below code is from the App.xaml, in the
Application.Resources section. The button is at the
bottom of the code.

I've been reading some on CommandBindings, and routed events.
Think that might be the way to go, but I'm not sure yet.
Gotta read some more.

<ControlTemplate x:Key="DefaultWindow" TargetType="{x:Type Window}">
<!-- Rounded black border -->
<Border BorderThickness="3" BorderBrush="Black" CornerRadius="10" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="templateBorder">
<Grid x:Name="templateGrid">
<!-- Rounded mask (stretches to fill Grid) -->
<Border CornerRadius="7" Grid.ZIndex="1" x:Name="innerTemplateBorder">
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0.001" ColorInterpolationMode="SRgbLinearInterpolation">
<RotateTransform Angle="90" CenterX="0.5" CenterY="0.5"/>
<!--<GradientStop Color="#FF006CFF" Offset="0.5"/>-->
<!--<GradientStop Color="#FF4E4E4E" Offset="0.5"/>
<GradientStop Color="LightGray" Offset="1"/>-->
<GradientStop Color="LightGray" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
<!-- Main content container -->
<Grid x:Name="innerTemplateGrid">
<RowDefinition Height="60" MaxHeight="60"/>
<RowDefinition Height="*"/>
<!--Here's the Icon handle-->
<Border Grid.Row="0" Grid.ZIndex="99" Width="121" Height="36" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Image Height="34" HorizontalAlignment="Left" Name="logoImage" Stretch="Fill" VerticalAlignment="Top" Width="120" Source="/MyNamespace;component/Images/somePicture.png" />
<!--Margin '46' because the above border has height 36 + a margin of 10.-->
<Border Grid.Row="0" Grid.ZIndex="99" Width="121" Margin="10,46,0,0" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top">
<VisualBrush Visual="{Binding ElementName=logoImage}">
<ScaleTransform ScaleX="1" ScaleY="-1" CenterX="60" CenterY="13"></ScaleTransform>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="Black"></GradientStop>
<GradientStop Offset="0.4" Color="Transparent"></GradientStop>
<Button Grid.Row="0" Focusable="False" Height="15" Width="15" HorizontalAlignment="Right" Margin="0,6,6,0" Name="btnClose"
Panel.ZIndex="13" VerticalAlignment="Top">
<!--<CommandBinding x:Name="closeCommandBinding" Command="{TemplateBinding CloseCommand}" />-->
<ImageBrush ImageSource="/MyNamespace;component/Images/windowClose.png" />
<!--This next line might show an error (e.g "not found") but it should work compile- and runtime, so don't worry about it.-->
<ContentPresenter Grid.Row="1" Grid.ZIndex="1" Content="{TemplateBinding Content}"/>

Thank you :)

Share this post

Link to post
Share on other sites
Commands are indeed the way to go in this case.

Implement a CloseCommand in the window class.

Share this post

Link to post
Share on other sites
Thank you ernow! :)

Figured it out, finally.
Thought I'd just explain here real
quick how I got it working.

My problem was that I read too many
posts by people who, I now realize,
didn't know what they were talking about.

Here's the article I found most helpful:

Hopefully this might help others in the same

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!