Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Button click event within a ControlTemplate?

Button click event within a ControlTemplate?

Scheduled Pinned Locked Moved WPF
csharpwpfquestionwcfhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Richard Dutton
    wrote on last edited by
    #1

    Hi, I have a class which derives from TextBox called EntryTextBox and provides a PreviewKeyDown event handler (although for the purposes of this question, it's not relevant). I have produced a ControlTemplate within a style as follows: <Style TargetType="{x:Type my:EntryTextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type my:EntryTextBox}"> <DockPanel> <Border BorderThickness="1" HorizontalAlignment="Stretch" BorderBrush="DarkGray" Background="White" Width="100"> <DockPanel> <Button DockPanel.Dock="Right" Visibility="{Binding Path=KeyboardPopup,Converter={StaticResource VisibilityConverter},RelativeSource={RelativeSource TemplatedParent}}"> <ContentControl Width="15" Height="10" Template="{StaticResource KeyboardIconContent}"/> </Button> <ScrollViewer Margin="0" Background="Transparent" HorizontalAlignment="Stretch" x:Name="PART_ContentHost"/> </DockPanel> </Border> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> Which as you can see displays a Button as part of the TextEntry box depending on the value of KeyboardPopup. This works fine, but I want to be able to act on the button's click handler from within my EntryTextBox class but don't know how to do it. I've read in "Pro WPF in C# 2008, Matthew MacDonald, P872" that you need to "give your elements recognisable names and attach event handlers to them programatically in the control constructor" but I am unsure of the syntax I should use in the class constructor of EntryTextBox. Can anyone help me? Cheers Richard

    R 1 Reply Last reply
    0
    • R Richard Dutton

      Hi, I have a class which derives from TextBox called EntryTextBox and provides a PreviewKeyDown event handler (although for the purposes of this question, it's not relevant). I have produced a ControlTemplate within a style as follows: <Style TargetType="{x:Type my:EntryTextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type my:EntryTextBox}"> <DockPanel> <Border BorderThickness="1" HorizontalAlignment="Stretch" BorderBrush="DarkGray" Background="White" Width="100"> <DockPanel> <Button DockPanel.Dock="Right" Visibility="{Binding Path=KeyboardPopup,Converter={StaticResource VisibilityConverter},RelativeSource={RelativeSource TemplatedParent}}"> <ContentControl Width="15" Height="10" Template="{StaticResource KeyboardIconContent}"/> </Button> <ScrollViewer Margin="0" Background="Transparent" HorizontalAlignment="Stretch" x:Name="PART_ContentHost"/> </DockPanel> </Border> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> Which as you can see displays a Button as part of the TextEntry box depending on the value of KeyboardPopup. This works fine, but I want to be able to act on the button's click handler from within my EntryTextBox class but don't know how to do it. I've read in "Pro WPF in C# 2008, Matthew MacDonald, P872" that you need to "give your elements recognisable names and attach event handlers to them programatically in the control constructor" but I am unsure of the syntax I should use in the class constructor of EntryTextBox. Can anyone help me? Cheers Richard

      R Offline
      R Offline
      rhuiden
      wrote on last edited by
      #2

      1. Name the button something line "PART_Button". 2. Then override OnApplyTemplate(). 3. In OnApplyTemplate use GetTemplateChild("PART_Button"). That will get you a reference to the button to add your event handler. Of course you will want to check the returned object to make sure is is not null. Someone may change the template for your control. ;)

      R 1 Reply Last reply
      0
      • R rhuiden

        1. Name the button something line "PART_Button". 2. Then override OnApplyTemplate(). 3. In OnApplyTemplate use GetTemplateChild("PART_Button"). That will get you a reference to the button to add your event handler. Of course you will want to check the returned object to make sure is is not null. Someone may change the template for your control. ;)

        R Offline
        R Offline
        Richard Dutton
        wrote on last edited by
        #3

        Thank-you rhuiden, you are a star! That's exactly what I was after. Richard

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups