locked
Templated Controls - handling event with control inside ControlTemplate and not just change VisualState

    Question

  • Hi, when it comes to working with Templated Controls I'm new.

    I want to expose an event delegate in my control that will fire on the "KeyDown" of the "InputTextBox"

    Because I wanted to control how the controls looked and acted through VisualState, I started making template controls. However, I'm not sure if I have the best case for a template control. However, this predicament has given me pause as to if and when to use template controls.

     The XAML that appears in the Generic.xaml files is here:

        <Style TargetType="AmsControls:LabeledTextBox1">
                  <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="AmsControls:LabeledTextBox1">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition Height="2*"/>
                                    <RowDefinition Height="Auto" MinHeight="10"/>
                                </Grid.RowDefinitions>
                                <TextBlock x:Name="CaptionTextBlock"  Text="{TemplateBinding Caption}"/>
                                <TextBox x:Name="InputTextBox" Text="{TemplateBinding InputValue}" Grid.Row="1" Foreground="{TemplateBinding TextBoxForground}" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    Please advise.

    Thanks!

    Monday, April 07, 2014 8:38 PM

Answers

  • Okay, I think the best thing to do is to separate the TextBlock and TextBox controls. If I want, style them separately and then include them in a CustomControl grouped to display properly then create any event handlers as necessary inside the CustomControl.

    I think I was asking too much of the Templated Control structure by using multiple controls and then trying to hook anything that I couldn't just override from the parent type.

    • Marked as answer by Ki6FHX Monday, April 07, 2014 9:16 PM
    Monday, April 07, 2014 9:07 PM