Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

WPF - How to wire up a Style Button to click Click Event

I have a window's resource Style.. that includes a Button.  The style is for a flow document reader.
The style shows find. How do I wire up a button that I've included in the Style to the flow document reader on a page.?  Please provide a simple example.   Here's the style.. need the Print button wired to the page that uses this control.. thanks

   <Style x:Key="documentViewerStyle" TargetType="{x:Type FlowDocumentReader}">
        <!--<Setter Property="BorderBrush" Value="{DynamicResource documentViewerStyle}"/>-->
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Margin" Value="5" />
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="Background" Value="White" />
        <Setter Property="ZoomIncrement" Value="5" />
        <Setter Property="ViewingMode" Value="Scroll" />
        <Setter Property="IsPageViewEnabled" Value="false" />
        <Setter Property="IsTwoPageViewEnabled" Value="false" />
        <Setter Property="IsScrollViewEnabled" Value="true" />
        <Setter Property="Focusable" Value="true" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type FlowDocumentReader}">
                 
                    <DockPanel Language="en-us" LastChildFill="True" Uid="DockPanel_3">

                         <Grid Background="{TemplateBinding Background}"  DockPanel.Dock="Top"  HorizontalAlignment="Center" Uid="Grid_31">
                             
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="200"/>
                                </Grid.ColumnDefinitions>

                                <StackPanel Grid.Column="1" Margin="5,2,0,0" Orientation="Horizontal" Uid="StackPanel_5">
                                    <ToggleButton x:Name="FindButton" Command="ApplicationCommands.Find" Focusable="False" Margin="3,0" ToolTip="Find" Uid="ToggleButton_1" Height="26.4" VerticalAlignment="Bottom">
                                        <ToggleButton.Style>
                                            <Style TargetType="{x:Type ToggleButton}">
                                                <Style.BasedOn>
                                                    <Style TargetType="{x:Type ButtonBase}">
                                                        <Setter Property="Focusable" Value="False"/>
                                                        <Setter Property="Opacity" Value="0.5"/>
                                                        <Setter Property="Cursor" Value="Hand"/>
                                                        <Setter Property="Background" Value="Transparent"/>
                                                        <Setter Property="Padding" Value="3,1"/>
                                                        <Setter Property="BorderBrush" Value="{x:Null}"/>
                                                        <Setter Property="BorderThickness" Value="0"/>
                                                        <Setter Property="MinWidth" Value="0"/>
                                                        <Setter Property="MinHeight" Value="0"/>
                                                        <Setter Property="Template">
                                                            <Setter.Value>
                                                                <ControlTemplate TargetType="{x:Type ButtonBase}">
                                                                    <Border Background="{TemplateBinding Background}" Uid="Border_39">
                                                                        <ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" RenderTransformOrigin="0.5,0.5" Uid="ContentPresenter_2"/>
                                                                    </Border>
                                                                    <ControlTemplate.Triggers>
                                                                        <Trigger Property="IsMouseOver" Value="True">
                                                                            <Setter Property="RenderTransform" TargetName="Content">
                                                                                <Setter.Value>
                                                                                    <ScaleTransform ScaleY="1.1" ScaleX="1.1"/>
                                                                                </Setter.Value>
                                                                            </Setter>
                                                                        </Trigger>
                                                                        <Trigger Property="IsPressed" Value="True">
                                                                            <Setter Property="RenderTransform" TargetName="Content">
                                                                                <Setter.Value>
                                                                                    <ScaleTransform ScaleY="0.9" ScaleX="0.9"/>
                                                                                </Setter.Value>
                                                                            </Setter>
                                                                        </Trigger>
                                                                    </ControlTemplate.Triggers>
                                                                </ControlTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                        <Style.Triggers>
                                                            <Trigger Property="IsEnabled" Value="False">
                                                                <Setter Property="Opacity" Value="0.3"/>
                                                            </Trigger>
                                                            <Trigger Property="IsMouseOver" Value="True">
                                                                <Setter Property="Opacity" Value="1"/>
                                                            </Trigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </Style.BasedOn>
                                                <Style.Triggers>
                                                    <Trigger Property="IsEnabled" Value="False">
                                                        <Setter Property="Visibility" Value="Collapsed"/>
                                                    </Trigger>
                                                    <Trigger Property="IsChecked" Value="True">
                                                        <Setter Property="Opacity" Value="1"/>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </ToggleButton.Style>
                                        <Path Stroke="{TemplateBinding Foreground}" Uid="Path_23" VerticalAlignment="Center">
                                            <Path.Data>
                                                <GeometryGroup>
                                                    <RectangleGeometry RadiusY="1" RadiusX="1" Rect="0.5,0.5,19,19"/>
                                                    <EllipseGeometry Center="12,8" RadiusY="5" RadiusX="5"/>
                                                    <EllipseGeometry Center="12,8" RadiusY="4" RadiusX="4"/>
                                                    <LineGeometry EndPoint="9,10" StartPoint="2.5,16.5"/>
                                                    <LineGeometry EndPoint="9.5,10.5" StartPoint="3,17"/>
                                                    <LineGeometry EndPoint="10,11" StartPoint="3.5,17.5"/>
                                                </GeometryGroup>
                                            </Path.Data>
                                        </Path>
                                    </ToggleButton>
                                    <Border x:Name="PART_FindToolBarHost" HorizontalAlignment="Left" Uid="Border_64" Visibility="Collapsed" VerticalAlignment="Center"/>
                                </StackPanel>



                                <Grid Grid.Column="0" HorizontalAlignment="Right" Uid="Grid_32" VerticalAlignment="Center">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="Auto"/>


                                    </Grid.ColumnDefinitions>

                                    <Image Grid.Column="1" Source="/LandonIP.PWBReader.BaseTypes;component/images/ZoomOut.png" />

                                    <Slider Grid.Column="2" TickPlacement="BottomRight"  IsSnapToTickEnabled="True" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="Stretch" LargeChange="{TemplateBinding ZoomIncrement}" Maximum="{TemplateBinding MaxZoom}" Minimum="{TemplateBinding MinZoom}" SmallChange="{TemplateBinding ZoomIncrement}" TickFrequency="{TemplateBinding ZoomIncrement}" Uid="Slider_3" Value="{Binding Zoom, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" Width="100">
                                        <Slider.Style>
                                            <Style TargetType="{x:Type Slider}">
                                                <Setter Property="Stylus.IsPressAndHoldEnabled" Value="False"/>
                                                <Setter Property="Focusable" Value="False"/>
                                                <Setter Property="OverridesDefaultStyle" Value="True"/>
                                                <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>
                                                <Setter Property="BorderThickness" Value="1"/>
                                                <Setter Property="BorderBrush" Value="#00000000"/>
                                                <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
                                                <Setter Property="IsSnapToTickEnabled" Value="True"/>
                                                <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
                                                <Setter Property="MinWidth" Value="50"/>
                                                <Setter Property="MinHeight" Value="0"/>
                                                <Setter Property="Width" Value="Auto"/>
                                                <Setter Property="Padding" Value="2,0"/>
                                                <Setter Property="Height" Value="Auto"/>
                                                <Setter Property="Opacity" Value="0.75"/>
                                                <Setter Property="Delay" Value="1000"/>
                                                <Setter Property="Interval" Value="250"/>
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate TargetType="{x:Type Slider}">
                                                            <Border Background="Transparent" Padding="{TemplateBinding Padding}" Uid="Border_45">
                                                                <Grid ClipToBounds="False" Uid="Grid_22">
                                                                    <Grid.ColumnDefinitions>
                                                                        <ColumnDefinition Width="*"/>
                                                                    </Grid.ColumnDefinitions>
                                                                    <Grid.RowDefinitions>
                                                                        <RowDefinition Height="Auto"/>
                                                                        <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
                                                                        <RowDefinition Height="Auto"/>
                                                                        <RowDefinition Height="*"/>
                                                                    </Grid.RowDefinitions>
                                                                    <TickBar x:Name="TopTick" Fill="{TemplateBinding Foreground}" Height="4" Placement="Top" Grid.Row="0" Uid="TickBar_1" Visibility="Collapsed"/>
                                                                    <Border Background="{TemplateBinding Foreground}" CornerRadius="2" Height="2" Opacity="0.5" Grid.Row="1" Uid="Border_46"/>
                                                                    <Canvas Height="4" Grid.Row="1" Uid="Canvas_7" VerticalAlignment="Center">
                                                                        <Rectangle x:Name="PART_SelectionRange" Fill="{TemplateBinding Foreground}" Height="2" Canvas.Top="1" Uid="Rectangle_17" Visibility="Hidden"/>
                                                                    </Canvas>
                                                                    <Track x:Name="PART_Track" Grid.Row="1" Uid="PART_Track">
                                                                        <Track.DecreaseRepeatButton>
                                                                            <RepeatButton Command="Slider.DecreaseLarge" Uid="RepeatButton_1">
                                                                                <RepeatButton.Style>
                                                                                    <Style TargetType="{x:Type RepeatButton}">
                                                                                        <Setter Property="OverridesDefaultStyle" Value="True"/>
                                                                                        <Setter Property="IsTabStop" Value="False"/>
                                                                                        <Setter Property="Focusable" Value="False"/>
                                                                                        <Setter Property="Template">
                                                                                            <Setter.Value>
                                                                                                <ControlTemplate TargetType="{x:Type RepeatButton}">
                                                                                                    <Border Background="Transparent" Uid="Border_43"/>
                                                                                                </ControlTemplate>
                                                                                            </Setter.Value>
                                                                                        </Setter>
                                                                                    </Style>
                                                                                </RepeatButton.Style>
                                                                            </RepeatButton>
                                                                        </Track.DecreaseRepeatButton>
                                                                        <Track.IncreaseRepeatButton>
                                                                            <RepeatButton Command="Slider.IncreaseLarge" Uid="RepeatButton_2">
                                                                                <RepeatButton.Style>
                                                                                    <Style TargetType="{x:Type RepeatButton}">
                                                                                        <Setter Property="OverridesDefaultStyle" Value="True"/>
                                                                                        <Setter Property="IsTabStop" Value="False"/>
                                                                                        <Setter Property="Focusable" Value="False"/>
                                                                                        <Setter Property="Template">
                                                                                            <Setter.Value>
                                                                                                <ControlTemplate TargetType="{x:Type RepeatButton}">
                                                                                                    <Border Background="Transparent" Uid="Border_43"/>
                                                                                                </ControlTemplate>
                                                                                            </Setter.Value>
                                                                                        </Setter>
                                                                                    </Style>
                                                                                </RepeatButton.Style>
                                                                            </RepeatButton>
                                                                        </Track.IncreaseRepeatButton>
                                                                        <Track.Thumb>
                                                                            <Thumb Background="{TemplateBinding Background}" Uid="Thumb_5">
                                                                                <Thumb.Style>
                                                                                    <Style TargetType="{x:Type Thumb}">
                                                                                        <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>
                                                                                        <Setter Property="Focusable" Value="False"/>
                                                                                        <Setter Property="OverridesDefaultStyle" Value="True"/>
                                                                                        <Setter Property="Cursor" Value="Hand"/>
                                                                                        <Setter Property="Padding" Value="6"/>
                                                                                        <Setter Property="Template">
                                                                                            <Setter.Value>
                                                                                                <ControlTemplate TargetType="{x:Type Thumb}">
                                                                                                    <Border Background="Transparent" Margin="-2" Padding="{Binding Padding, RelativeSource={RelativeSource TemplatedParent}}" Uid="Border_44">
                                                                                                        <Viewbox HorizontalAlignment="Center" Uid="Viewbox_4" VerticalAlignment="Center" Width="{Binding FontSize, RelativeSource={RelativeSource TemplatedParent}}">
                                                                                                            <Viewbox.LayoutTransform>
                                                                                                                <ScaleTransform ScaleY="1" ScaleX="1"/>
                                                                                                            </Viewbox.LayoutTransform>
                                                                                                            <Canvas Height="17" Uid="Canvas_6" Width="13">
                                                                                                                <Path Data="F1 M1,0 L10,0 L11,1 L11,13 L6.5,17 L4.5,17 L0,13 L0,1 L1,0 z" Fill="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" Uid="Path_13"/>
                                                                                                                <Path Data="F1 M1,0 L10,0 L11,1 L11,13 L6.5,17 L4.5,17 L0,13 L0,1 L1,0 z" StrokeEndLineCap="Round" StrokeDashCap="Round" Stroke="{TemplateBinding Background}" StrokeThickness="0.75" StrokeMiterLimit="1" StrokeLineJoin="Round" Uid="Path_14">
                                                                                                                    <Path.Fill>
                                                                                                                        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                                                                                                            <LinearGradientBrush.RelativeTransform>
                                                                                                                                <TransformGroup>
                                                                                                                                    <TranslateTransform X="-0.5" Y="-0.5"/>
                                                                                                                                    <ScaleTransform ScaleY="1" ScaleX="1"/>
                                                                                                                                    <SkewTransform AngleY="0" AngleX="0"/>
                                                                                                                                    <RotateTransform Angle="110"/>
                                                                                                                                    <TranslateTransform X="0.5" Y="0.5"/>
                                                                                                                                </TransformGroup>
                                                                                                                            </LinearGradientBrush.RelativeTransform>
                                                                                                                            <GradientStop Color="#CC999999" Offset="0.5"/>
                                                                                                                            <GradientStop Color="#CCFFFFFF" Offset="0.8"/>
                                                                                                                            <GradientStop Color="#CCFFFFFF" Offset="0.2"/>
                                                                                                                        </LinearGradientBrush>
                                                                                                                    </Path.Fill>
                                                                                                                </Path>
                                                                                                            </Canvas>
                                                                                                        </Viewbox>
                                                                                                    </Border>
                                                                                                </ControlTemplate>
                                                                                            </Setter.Value>
                                                                                        </Setter>
                                                                                    </Style>
                                                                                </Thumb.Style>
                                                                            </Thumb>
                                                                        </Track.Thumb>
                                                                    </Track>
                                                                    <TickBar x:Name="BottomTick" Fill="{TemplateBinding Foreground}" TickFrequency="75" Height="10" Placement="Bottom" Grid.Row="1" Uid="TickBar_2" Visibility="Collapsed"/>
                                                                </Grid>
                                                            </Border>
                                                            <ControlTemplate.Triggers>
                                                                <Trigger Property="TickPlacement" Value="TopLeft">
                                                                    <Setter Property="Visibility" TargetName="TopTick" Value="Visible"/>
                                                                </Trigger>
                                                                <Trigger Property="TickPlacement" Value="BottomRight">
                                                                    <Setter Property="Visibility" TargetName="BottomTick" Value="Visible"/>
                                                                </Trigger>
                                                                <Trigger Property="TickPlacement" Value="Both">
                                                                    <Setter Property="Visibility" TargetName="TopTick" Value="Visible"/>
                                                                    <Setter Property="Visibility" TargetName="BottomTick" Value="Visible"/>
                                                                </Trigger>
                                                                <Trigger Property="IsSelectionRangeEnabled" Value="True">
                                                                    <Setter Property="Visibility" TargetName="PART_SelectionRange" Value="Visible"/>
                                                                </Trigger>
                                                                <Trigger Property="IsMouseOver" Value="True">
                                                                    <Setter Property="RenderTransform">
                                                                        <Setter.Value>
                                                                            <ScaleTransform ScaleY="1.05" ScaleX="1.05"/>
                                                                        </Setter.Value>
                                                                    </Setter>
                                                                </Trigger>
                                                                <Trigger Property="IsMouseOver" Value="True">
                                                                    <Setter Property="Opacity" Value="1"/>
                                                                    <Setter Property="RenderTransform">
                                                                        <Setter.Value>
                                                                            <ScaleTransform ScaleY="1.05" ScaleX="1.05"/>
                                                                        </Setter.Value>
                                                                    </Setter>
                                                                </Trigger>
                                                                <Trigger Property="IsEnabled" Value="False">
                                                                    <Setter Property="Opacity" Value="0.3"/>
                                                                </Trigger>

                                                            </ControlTemplate.Triggers>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
                                        </Slider.Style>
                                    </Slider>
                                    <Image Grid.Column="3" Source="/LandonIP.PWBReader.BaseTypes;component/images/ZoomIn.png" />

                                </Grid>


                                <Grid Grid.Column="2">

                                    <StackPanel Orientation="Horizontal" Margin="5,0,0,0">

                                        <Button x:Name="btnPrint" Command="{x:Static FlowDocumentReader:AppCommands.Print}"

                                        Height="25" Width="70" ToolTip="Print"  Margin="5,0,0,0" >
                                            <StackPanel Orientation="Horizontal" >
                                                <Image  Source="/LandonIP.PWBReader.BaseTypes;component/images/Print.png"
                                         Height="24" Width="24"/>
                                                <TextBlock Text="Print" FontSize="12" Margin="2,2,0,0"/>
                                            </StackPanel>
                                        </Button>

                                        <Button  x:Name="btnExport" Command="{Binding ElementName=root, Path=Export}" Height="25" Width="70" ToolTip="Export" Margin="8,0,0,0" >
                                            <StackPanel Orientation="Horizontal" >
                                                <Image  Source="/LandonIP.PWBReader.BaseTypes;component/images/Move.png" Height="24" Width="24" />
                                                <TextBlock Text="Export" FontSize="12" Margin="2,2,0,0"/>
                                            </StackPanel>
                                        </Button>

                                    </StackPanel>
                                </Grid>

                            </Grid>
                 
                       
                        <!-- CONTENT !!!-->
                        <Grid Uid="Grid_33">
                            <Border x:Name="PART_ContentHost" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" KeyboardNavigation.IsTabStop="False" Margin="{TemplateBinding Padding}" KeyboardNavigation.TabIndex="1" KeyboardNavigation.TabNavigation="Local" Uid="Border_59"/>
                        </Grid>
                     

                    </DockPanel>
                   
                    <ControlTemplate.Triggers>
                        <Trigger Property="ViewingMode" Value="Page"/>
                        <Trigger Property="ViewingMode" Value="TwoPage"/>
                        <Trigger Property="ViewingMode" Value="Scroll">
                            <!--<Setter Property="Visibility" TargetName="PageCountAndButtons" Value="Collapsed"/>-->
                        </Trigger>
                        <Trigger Property="Document" Value="{x:Null}">
                            <Setter Property="IsEnabled" TargetName="FindButton" Value="False"/>
                        </Trigger>
                        <Trigger Property="IsFindEnabled" Value="False">
                            <Setter Property="IsEnabled" TargetName="FindButton" Value="False"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
              </Setter.Value>
        </Setter>
    </Style>
Avatar of saragani
saragani

The button is already wired up to a click "action":

<Button x:Name="btnPrint" Command="{x:Static FlowDocumentReader:AppCommands.Print}"
                                        Height="25" Width="70" ToolTip="Print"  Margin="5,0,0,0" >

It has the Command binded to the AppCommands.Print

That should open the Print dialog.



You can't put any OnClick even on a style or control template since those are defined in a different XAML which can be applied to different FlowDocumentReader's located on different windows or user controls...
There is no guarantee that that code-behind will have the ButtonClick written, so it is not possible to use Events.

You can how ever use Commands, since commands use Binding.


If you like to run commands or some methods when some event happens, you can consider using Blend.
Blend 4.0 (I think that also 3.0) gives you the ability to use some of it's DLLs for "Interaction".
In this way you can use Behaviors like CallMethodAction for running Code in your ViewModel / Data Context.
I wrote OnClick even, I forgot the t, it should be: OnClick event
Avatar of JElster

ASKER

I need to bind to a custom command.. not the 'built in' print...
I need to bind it to a PrintDoc() which each page will have.
How do I do that?
thanks
ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial