从 XAML 调用位于类中的事件或方法 [英] Call events or methods located in a Class from XAML

查看:67
本文介绍了从 XAML 调用位于类中的事件或方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试是否有可能在另一个独立于 Window CodeBehind(如类)的地方放置一个像 TextChanged(一个 TextBox)这样的事件.

Hi i am trying if it's possible having an Event like TextChanged (of a TextBox) located in another place independent of the Window CodeBehind (like a Class).

我想要做的是在 ResourceDictionary 中有一个对 TextBox 事件的引用.(因为 ResourcesDictionaries 没有 CodeBehind.)

What i am trying to do is having in the ResourceDictionary a reference to an event of the TextBox. (Because the ResourcesDictionaries doesn't have a CodeBehind.)

注意:确实必须通过这种方式,因为我正在自定义另一个控件以动态地拥有一些 TextBox,并且当 TextChanged 发生时,所有 TextBox 都会触发相同的事件.

Note: It really have to be by this way, because i am customizing another control to have some TextBoxes dynamically and all the TextBoxes will fire the same event when TextChanged occurs.

推荐答案

我建议使用触发器.您可以在此处阅读有关它们的更多信息:http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx

I would suggest triggers. You can read more about them here: http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx

本质上,您可以在 xaml 中将控件的事件挂钩到触发器,并让它在其他地方执行方法或命令.

Essentially you can, in xaml, hook a control's event to a trigger and have it execute a method or command else where.

我应该注意,虽然它确实说Silverlight",但它也适用于 WPF.Edit2:MVVM 工具包提供了一个 EventToCommandTrigger 可以让你做这样的事情:

I should note that while it does say 'Silverlight' it all applies to WPF as well. The MVVM toolkit provides an EventToCommandTrigger that would let you do something like this:

http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

感谢 H.B. 的回答提醒我这样做的命令方式

Kudos to H.B.'s answer for reminding me about the command way of doing this

Edit3:更好的例子,这在很大程度上借鉴了 MVVM 世界的做法.由于 EventToCommand 绑定将附加到控件的任何上下文,因此您可以将其粘贴在 ResourceDictionary 中以及放置它的任何位置,它会尝试找到该 TextChangeCommand 属性.

Better example, this borrows heavily from how the MVVM world would do this. Since the EventToCommand binding would attached to whatever the control's context is, you could stick this in your ResourceDictionary and anywhere you place it, it would attempt to find that TextChangeCommand property.

 <Window x:Class="TestBed.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras">
    <Grid>
        <TextBox x:Name = "MyTextBox">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <cmd:EventToCommand Command="{Binding TextChanged, Mode=OneWay}"
                                        CommandParameter="{Binding Text, 
                                              ElementName=MyTextBox, Mode=OneWay}"
                        MustToggleIsEnabledValue="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
    </Grid>
</Window>

和代码隐藏:

public partial class TestWindow : Window
{
    public TestWindow()
    {
        this.TextChangedCommand = new RelayCommand<string>(
            (str) => TextChanged(str));

        InitializeComponent();
    }

    public RelayCommand<string> TextChangedCommand
    {
        get;
        private set;
    }

    public void TextChanged(string str)
    {
    }
}

这篇关于从 XAML 调用位于类中的事件或方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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