简体   繁体   English

如何对模板控件中的按钮单击做出反应

[英]How to react on a button click in a Templated Control

I have written a custom Templated Control which contains a button: 我编写了一个自定义模板控件,其中包含一个按钮:

<ControlTemplate TargetType="local:IncrementalFlipView">
    <Border
        Background="{TemplateBinding Background}"
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}">

        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
            <Button Content="^" x:Name="PreviousPageButton" Click="Button_Click"/>
            <TextBlock Text="1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0" FontSize="16" />
            <Button Content="v" x:Name="NextPageButton"/>
        </StackPanel>

    </Border>
</ControlTemplate>

My problem is, that I don't know how to react on the button's click event. 我的问题是,我不知道如何对按钮的单击事件做出反应。 I implemented a Button_Click(object sender, RoutedEventArgs e) eventhandler in my Template Control's code but it does not catch the button's click event. 我在模板控件的代码中实现了Button_Click(object sender, RoutedEventArgs e)事件处理程序,但未捕获按钮的click事件。

public sealed class IncrementalFlipView : Control
{
    public IncrementalFlipView()
    {
        this.DefaultStyleKey = typeof(IncrementalFlipView);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Clicked!");
    }
}

Any idea how to react on that clicked event? 知道如何应对该点击事件吗?

The correct way (my point of view) would be to use Command Bindings . 正确的方法(我的观点)是使用Command Bindings Your styled button in your Templated control will look something like this: 您的模板化控件中的样式化按钮将如下所示:

 <Button Command="{Binding DataContext.BackCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type YourUIType}}}"                                      CommandParameter="{Binding}" />

The type of your AncestorType depends on the UI element that you are using. AncestorType的类型取决于您使用的UI元素。

In your main Window you should define the Command : 在主窗口中,应定义Command

private RelayCommand _backCommand;
public ICommand BackCommand
{
    get { return _backCommand ?? (_backCommand = new RelayCommand(Back)); }
}

If you go through the Internet you will see the basic RelayCommand definition for the ICommand interface (see [here]). 如果您通过Internet访问,则将看到ICommand接口的基本RelayCommand定义(请参见[此处])。 1 1个

And then, you only have to define the Back method (it has a paramethrized object): 然后,您只需要定义Back方法(它具有一个被甲基化的对象):

private void Back(object obj)
{
    // Go back command
    ...
}

Ah okay, I solved it by myself. 好的,我自己解决了。 It is not possible to pass the Clicked event to the base class. 无法将Clicked事件传递给基类。 But one can access the button when the template is loaded and let him subscribe the clicked event like this: 但是当模板加载时,可以访问该按钮,并让他订阅如下所示的clicked事件:

protected override void OnApplyTemplate()
{
    Button PreviousPageButton = (Button)GetTemplateChild("PreviousPageButton");
    PreviousPageButton.Click += PreviousPageButton_Click;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM