从XAML调用位于类中的事件或方法

| 嗨,我正在尝试是否有一个事件,例如TextChanged(文本框的)位于独立于Window CodeBehind(例如Class)的另一个位置。 我想做的是在ResourceDictionary中具有对TextBox事件的引用。 (因为ResourcesDictionaries中没有CodeBehind。) 注意:确实必须通过这种方式,因为我正在自定义另一个控件以动态地具有一些TextBoxes,并且当发生TextChanged时,所有TextBoxes都会触发相同的事件。     
已邀请:
我建议触发器。您可以在这里阅读有关它们的更多信息:http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx 本质上,您可以在xaml中将控件的事件挂接到触发器,并让它在其他位置执行方法或命令。 编辑:我应该注意,尽管它确实说了“'Silverlight \'”,但这同样适用于WPF。 Edit2:MVVM工具箱提供了一个EventToCommandTrigger,它可以让您执行以下操作: http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx H.B.的答案让我想起了执行此命令的方式 Edit3:更好的例子,这是MVVM世界如何做到这一点的重要借鉴。由于EventToCommand绑定将附加到控件上下文的任何内容,因此可以将其粘贴在ResourceDictionary中以及放置它的任何位置,它将尝试找到该TextChangeCommand属性。
 <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)
    {
    }
}
    
编辑:除非您想继承TextBox以便在TextChanged上执行命令,否则请忽略此设置。 (我认为触发方法是首选。)   但是有可能在   事件的资源字典   位于代码的背后   该控件所属的窗口?   想象以下,而不是我们   正在将事件设置为   XAML,我们在字典中进行   以一种风格。有可能吗 我会为此建议命令;在某处定义一个RoutedCommand:
public static class Commands
{
    public static RoutedCommand DoStuff = new RoutedCommand();
}
将其设置为字典中的按钮:
<Button x:Key=\"TheButton\" Content=\"Click\"
        Command=\"{x:Static local:Commands.DoStuff}\" />
并创建命令绑定:
<Grid>
    <Grid.CommandBindings>
        <CommandBinding Command=\"{x:Static local:Commands.DoStuff}\"
                        Executed=\"DoStuff_Executed\"
                        CanExecute=\"DoStuff_CanExecute\"/>
    </Grid.CommandBindings>
    <StaticResource ResourceKey=\"TheButton\"/>
</Grid>
private void DoStuff_Executed(object sender, ExecutedRoutedEventArgs e)
{
    // What happens if the command is executed, in this case the Button-click can
    // cause this to happen, you can also create KeyBindings which can execute
    // commands for example.
    MessageBox.Show(\"!\");
}
private void DoStuff_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true; //No condition: Command can always be executed

    // e.CanExecute = false causes the button to be disabled.
}
    

要回复问题请先登录注册