Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Template close button

Started by C3rial Oct 20, 2010 at 3:00 AM 3 replies 1.4k views
Original Post
C3rial
C3rial
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 :)
CrazyPoly
ernow
ernow
Please post the code/xaml that shows where and how you added the Close button. A lot depends on that.
C3rial
C3rial
Hi!

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.

[Source] <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">                        <Border.Background>                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0.001" ColorInterpolationMode="SRgbLinearInterpolation">                                <LinearGradientBrush.Transform>                                    <RotateTransform Angle="90" CenterX="0.5" CenterY="0.5"/>                                </LinearGradientBrush.Transform>                                <!--<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"/>                            </LinearGradientBrush>                        </Border.Background>                        <!-- Main content container -->                        <Grid x:Name="innerTemplateGrid">                            <Grid.RowDefinitions>                                <RowDefinition Height="60" MaxHeight="60"/>                                <RowDefinition Height="*"/>                            </Grid.RowDefinitions>                            <!--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" />                            </Border>                            <!--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">                                <Border.Background>                                    <VisualBrush Visual="{Binding ElementName=logoImage}">                                        <VisualBrush.Transform>                                            <ScaleTransform ScaleX="1" ScaleY="-1" CenterX="60" CenterY="13"></ScaleTransform>                                        </VisualBrush.Transform>                                    </VisualBrush>                                </Border.Background>                                <Border.OpacityMask>                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">                                        <GradientStop Offset="0" Color="Black"></GradientStop>                                        <GradientStop Offset="0.4" Color="Transparent"></GradientStop>                                    </LinearGradientBrush>                                </Border.OpacityMask>                            </Border>                            <Button Grid.Row="0" Focusable="False" Height="15" Width="15" HorizontalAlignment="Right" Margin="0,6,6,0" Name="btnClose"                                         Panel.ZIndex="13" VerticalAlignment="Top">                                <Button.CommandBindings>                                    <!--<CommandBinding x:Name="closeCommandBinding" Command="{TemplateBinding CloseCommand}" />-->                                </Button.CommandBindings>                                <Button.Background>                                    <ImageBrush ImageSource="/MyNamespace;component/Images/windowClose.png" />                                </Button.Background>                            </Button>                            <!--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}"/>                        </Grid>                    </Border>                </Grid>            </Border>        </ControlTemplate>        [/Source]


Thank you :)
CrazyPoly
ernow
ernow
Commands are indeed the way to go in this case.

Implement a CloseCommand in the window class.
C3rial
C3rial
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:
http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

Hopefully this might help others in the same
situation.
CrazyPoly

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.