简体   繁体   English

具有自定义控件的简单WPF事件问题

[英]Simple WPF event question with custom control

Im making a custom button called "ShardButton" which uses a Style to control its content as well as its event triggers: 我制作了一个名为“ ShardButton”的自定义按钮,该按钮使用一种样式来控制其内容以及事件触发器:

<Style x:Key="ButtonStyle" TargetType="{x:Type obj:ShardButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type obj:ShardButton}">
                <Grid>
                    <Path .../>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                      RecognizesAccessKey="True"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="ButtonBase.Click">
                        !!!SOMETHING HERE!!!
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Im using the above XAML in the main Window1.xaml file, and creating a xmlns to the "obj" namespace, so what I want to do is to call a custom event handling method on the ShardButton class, how do I do that in XAML? 我在主Window1.xaml文件中使用上述XAML,并为“ obj”命名空间创建了一个xmlns,所以我想做的是在ShardButton类上调用自定义事件处理方法,我该如何在XAML中做到这一点?

I cant seem to find the right way to phrase it for a Google search... 我似乎找不到为Google搜索表达其用语的正确方法。

Thanks for any help you can give me! 感谢你给与我的帮助!

Mark 标记

You can't directly execute event handlers in EventTriggers, they were'nt created for this purpose. 您不能直接在EventTriggers中执行事件处理程序,因为它们不是为此目的而创建的。

However, if every instance of your ShardButton must do the same thing on click, simply override the OnClick method in your ShardButton class and do whatever you have to do here. 但是,如果您的ShardButton的每个实例都必须在单击时执行相同的操作,则只需覆盖ShardButton类中的OnClick方法,然后执行此处要做的任何事情。

If you don't desire this behavior but instead want to have a specific handler executed for each ShardButton instance that is using this specific style defined in Window1.xaml, add another setter: 如果您不希望这种行为,而是希望为每个使用Window1.xaml中定义的特定样式的ShardButton实例执行特定的处理程序,请添加另一个设置器:

<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type obj:Window1}}, Path=MyCommand}" />

Where MyCommand is an instance property of Window1 returning a custom implementation of ICommand that will invoke your handler. 其中MyCommand是Window1的实例属性,它返回ICommand的自定义实现,该实现将调用您的处理程序。 This line of code assumes that every button using this Style is somewhere in the visual tree of Window1, which should be probably the case (or the Style would be defined elsewhere). 此行代码假定使用此样式的每个按钮都在Window1的可视树中的某个位置,这可能是这种情况(否则样式将在其他位置定义)。

You can use an EventSetter in your Style : 您可以在自己的Style使用EventSetter

<Style ...>
    <EventSetter Event="Click" Handler="yourHandlerMethod"/>
</Style>

Then in your code-behind: 然后在您的代码背后:

private void yourHandlerMethod(object sender, RoutedEventArgs e)
{
    ...
}

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

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