Button click event within a ControlTemplate?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Button click event within a ControlTemplate?

  1. #1
    Join Date
    Jun 2009
    Posts
    6

    Button click event within a ControlTemplate?

    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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,196

    Re: Button click event within a ControlTemplate?

    I'd use a routed ui command

    Create the command
    Code:
    public static classAppCommands
    {
      publicstaticRoutedUICommand KbPopup = newRoutedUICommand("_Popup", "KbPopup", typeof(AppCommands));
    }



    Use it in the template
    Code:
    <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" 
    Command="{x:Static my:AppCommands.KbPopup}"
     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>
    


    Wire up the command bindings in the UserControl
    Code:
    <UserControl.CommandBindings>
      <CommandBinding Command="{x:Static my:AppCommands.KbPopup}" CanExecute="OnKbPopupCanExecute" Executed="OnAddSchedule" />
    </UserControl.CommandBindings>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center