Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / WPF

Commands in MVVM

Rate me:
Please Sign up or sign in to vote.
4.97/5 (107 votes)
3 Dec 2012CPOL15 min read 509.7K   16.9K   279  
A consistent approach to Commands, Asynchronous Commands, and Events-to-Commands for WPF, Silverlight, and WP7.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:apexControls="clr-namespace:Apex.Controls"
    xmlns:apexCommands="clr-namespace:Apex.Commands"
    xmlns:apexDesign="clr-namespace:Apex.Design">
    <Style TargetType="{x:Type apexControls:CrossButton}">
        <Style.Resources>
            <SolidColorBrush x:Key="TabItemCloseButtonNormalBackgroundBrush" Color="#00000000" />
            <SolidColorBrush x:Key="TabItemCloseButtonNormalBorderBrush" Color="#FFFFFFFF" />
            <SolidColorBrush x:Key="TabItemCloseButtonNormalForegroundBrush" Color="#FF8f949b" />

            <SolidColorBrush x:Key="TabItemCloseButtonHoverBackgroundBrush" Color="#FFc13535" />
            <SolidColorBrush x:Key="TabItemCloseButtonHoverForegroundBrush" Color="#FFf9ebeb" />

            <SolidColorBrush x:Key="TabItemCloseButtonPressedBackgroundBrush" Color="#FF431e20" />
            <SolidColorBrush x:Key="TabItemCloseButtonPressedBorderBrush" Color="#FF110033" />
            <SolidColorBrush x:Key="TabItemCloseButtonPressedForegroundBrush" Color="#FFf9ebeb" />
        </Style.Resources>
        <Setter Property="SnapsToDevicePixels" Value="false" />
        <!--    <Setter Property="Height" Value="16" />
        <Setter Property="Width" Value="16" /> -->
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="Transparent">
                        <Ellipse x:Name="backgroundEllipse" />
                        <!-- The Path below will render the button's X. -->
                        <Path x:Name="ButtonPath"
                              Margin="3"
                              Stroke="{StaticResource TabItemCloseButtonNormalForegroundBrush}"
                              StrokeThickness="1.5"
                              StrokeStartLineCap="Square"
                              StrokeEndLineCap="Square"
                              Stretch="Uniform"
                              VerticalAlignment="Center"
                              HorizontalAlignment="Center">
                            <Path.Data>
                                <PathGeometry>
                                    <PathGeometry.Figures>
                                        <PathFigure StartPoint="0,0">
                                            <LineSegment Point="25,25"/>
                                        </PathFigure>
                                        <PathFigure StartPoint="0,25">
                                            <LineSegment Point="25,0"/>
                                        </PathFigure>
                                    </PathGeometry.Figures>
                                </PathGeometry>
                            </Path.Data>
                        </Path>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="backgroundEllipse"
                          Property="Fill"
                          Value="{StaticResource
                             TabItemCloseButtonHoverBackgroundBrush}" />
                            <Setter TargetName="ButtonPath"
                          Property="Stroke"
                          Value="{StaticResource
                             TabItemCloseButtonHoverForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="backgroundEllipse"
                                Property="Fill"
                                Value="{StaticResource
                                   TabItemCloseButtonPressedBackgroundBrush}" />
                            <Setter TargetName="backgroundEllipse"
                                Property="Stroke"
                                Value="{StaticResource
                                   TabItemCloseButtonPressedBorderBrush}" />
                            <Setter TargetName="ButtonPath" Property="Stroke"
                                Value="{StaticResource
                                   TabItemCloseButtonPressedForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions