如何将事件添加到样式(资源)? [英] How to add event to the style(resource)?

查看:68
本文介绍了如何将事件添加到样式(资源)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此示例来进行文本框验证。这是代码。

I am using this example to do textbox validation. Here is the code.

<Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">

                        <TextBlock DockPanel.Dock="Right"
                            Foreground="Orange"
                            Margin="5" 
                            FontSize="12pt"
                            Text="{Binding ElementName=MyAdorner, 
                           Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                           >
                        </TextBlock>

                        <Border BorderBrush="Green" BorderThickness="3">
                            <AdornedElementPlaceholder Name="MyAdorner" />
                        </Border>

                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={RelativeSource Self}, 
                   Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

</Application.Resources>



现在我要添加一个鼠标点击它。此外,我想将此样式移动到单独的样式文件。



问题是资源文件中不能包含事件。怎么样?



我尝试了什么:



找到这个一个

但它似乎使用了类似的代码。可能没关系。但是我希望模板可以重用。


Now I am going to add a mouse click even to it. Also I want to move this style to the a separate style file.

The question is that resource file can't have event in it. How?

What I have tried:

Find this one.
But it seems to use code behind something like. It might be okay. However I want the template can be re-useful.

推荐答案

您可以使用许多不同类型的触发器。在您的示例中,您正在使用数据触发器。对于Event,你需要使用EventTrigger。



我在本月早些时候写了一篇关于ToggleSwitch控件的WPF文章。在其中我分享了从Windows 10复制通知设置屏幕的示例。在文章XAML代码片段中,我展示了如何使用MouseTver的EventTrigger。这是一个理想的例子: C#中的灵活WPF ToggleSwitch无视控制& VB [ ^ ] - 下载代码,你可以看到它是如何工作的。
There are a number of different types of Triggers that you can use. In your example, you are using a Data Trigger. For Event, you need to use an EventTrigger.

I wrote an WPF article earlier this month about a ToggleSwitch control. In it I share an example of replicating the Notification Settings screen from Windows 10. In the article XAML code snippet I show how to use the EventTrigger for MouseOver. This is an ideal example for you: Flexible WPF ToggleSwitch Lookless Control in C# & VB[^] - download the code and you can see how it works.


这篇关于如何将事件添加到样式(资源)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆