0

I created a new CustomControl and rewrite the controltemplate.

Here is the controltemplate:

<Style TargetType="{x:Type local:CustomControl1}">        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border >
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"></RowDefinition>
                                <RowDefinition Height="auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <TextBlock ></TextBlock>
                            <Button Grid.Row="1"></Button>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

As you see,I used a button in the ControlTemplate,and I wants to use the click event of this button.However,it seems visual studio forbid me to using it.

PS:I wrote the ControlTemplate in Generic.xaml which visual studio auto created when I create a new CustomControl project.What's more,I am not using the English version Visual Studio,so I can only tell you the general meaning what Visual Studio said,I am very sorry about it.

How can I use the click event of the button in controltemplate?Thank you.

Melon NG
  • 2,568
  • 6
  • 27
  • 52
  • Better to use a Command. But **1.** where is the ControlTemplate defined? **2.** When you say *"it seems visual studio forbid me to using it"*, that sounds like you perceived something in your environment -- visual, auditory, maybe even olfactory or proprioceptive -- some form of information, conveyed to you by one or more of your senses, which appeared to originate in Visual Studio in some way, and which you interpreted as a message somehow indicating that Visual Studio wouldn't allow you to "use" an event. What information was that? What event? "Use" it how? **Be specific.** – 15ee8f99-57ff-4f92-890c-b56153 May 25 '18 at 14:11
  • @EdPlunkett When I created a new CustomControl project,visual studio will auto create a file named Generic.xaml.I wrote the ControlTemplate in it and the ControlTemplate worked.However as I said before,it seems can not use event in Generic.xaml. – Melon NG May 25 '18 at 23:02
  • @EdPlunkett I knowed I haven't show what the visual studio forbid me to using it,beaucase I am using the Chinese version visual studio and I don't know what if English version will show.But the meaning what VS reported is forbid using event. – Melon NG May 25 '18 at 23:04
  • @EdPlunkett I just tried the way which mm8 showed in answer,that's what I need.And I will accept the answer.Thank you. – Melon NG May 25 '18 at 23:08

1 Answers1

2

Where do you define the Style with the template?

If it's defined in a view, you could define an event handler in the code-behind of the view and hook it up as usual.

If it's defined in a ResourceDictionary, you could do the same thing provided that you actually add a code-behind class to your ResourceDictionary:

Is it possible to set code behind a resource dictionary in WPF for event handling?

But a better solution would be to hook up the event handler in your custom control class' OnApplyTemplate() method:

public class CustomControl1 : Control
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        Button btn = GetTemplateChild("btn") as Button;
        if(btn != null)
        {
            btn.Click += Btn_Click;
        }
    }

    private void Btn_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        //handle click...
    }
}

Just remember to give the Button an x:Name in the ControlTemplate:

<ControlTemplate TargetType="{x:Type local:CustomControl1}">
    <Border>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="auto"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock ></TextBlock>
            <Button x:Name="btn" Grid.Row="1"></Button>
        </Grid>
    </Border>
</ControlTemplate>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I tried your way and it worked!I defined the Style with the template in Generic.xaml which Visual Studio auto created when I create a new CustomControl project.Thank you! – Melon NG May 25 '18 at 23:10