【问题标题】:WPF - command via events [duplicate]WPF - 通过事件的命令[重复]
【发布时间】:2014-12-04 13:22:10
【问题描述】:

WPF - 通过事件命令

我已经通过 Command 处理了 TextChanged 事件。

<TextBox Text="{Binding ConnectionString, Mode=TwoWay}" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged">
                        <i:InvokeCommandAction Command="{Binding Path=SomeCommand, Mode=OneWay}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>

我的参考资料:

- Microsoft.Expression.Interactions
- Systsem.Windwos.Interactivity

例外:

类型的第一次机会异常 'System.Windows.Markup.XamlParseException' 发生在 PresentationFramework.dll

附加信息:无法加载文件或程序集 'System.Windows.Interactivity, PublicKeyToken=31bf3856ad364e35' 或一 其依赖项。系统找不到指定的文件。

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    这对你有用吗?

    XAML-

    <Window x:Class="garbage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Text="{Binding ConnectionString, Mode=TwoWay}" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding Path=SomeCommand, Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
    </Grid>
    

    后面的代码

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace garbage
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = this;
                SomeCommand = new RelayCommand(SomeCommandExecute, CanExecute);
            }
    
            private ICommand someCommand;
            public ICommand SomeCommand
            {
                get
                {
                    return someCommand;
                }
                set
                {
                    someCommand = value;
                }
            }
    
            public void SomeCommandExecute(object something)
            {
                MessageBox.Show("Success");
            }
    
            private bool CanExecute(object thing)
            { return true; }
    
        }
    }
    

    RelayCommand.cs

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;
    
    namespace garbage
    {
        public class RelayCommand : ICommand
        {
            private Action<object> execute;
    
            private Predicate<object> canExecute;
    
            private event EventHandler CanExecuteChangedInternal;
    
            public RelayCommand(Action<object> execute)
                : this(execute, DefaultCanExecute)
            {
            }
    
            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                if (execute == null)
                {
                    throw new ArgumentNullException("execute");
                }
    
                if (canExecute == null)
                {
                    throw new ArgumentNullException("canExecute");
                }
    
                this.execute = execute;
                this.canExecute = canExecute;
            }
    
            public event EventHandler CanExecuteChanged
            {
                add
                {
                    CommandManager.RequerySuggested += value;
                    this.CanExecuteChangedInternal += value;
                }
    
                remove
                {
                    CommandManager.RequerySuggested -= value;
                    this.CanExecuteChangedInternal -= value;
                }
            }
    
            public bool CanExecute(object parameter)
            {
                return this.canExecute != null && this.canExecute(parameter);
            }
    
            public void Execute(object parameter)
            {
                this.execute(parameter);
            }
    
            public void OnCanExecuteChanged()
            {
                EventHandler handler = this.CanExecuteChangedInternal;
                if (handler != null)
                {
                    //DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));
                    handler.Invoke(this, EventArgs.Empty);
                }
            }
    
            public void Destroy()
            {
                this.canExecute = _ => false;
                this.execute = _ => { return; };
            }
    
            private static bool DefaultCanExecute(object parameter)
            {
                return true;
            }
        }
    }
    

    【讨论】:

    • "'Expression.Blend.Sdk 1.0.2' 已经安装。" TargetFramework:.NET 框架 4.5
    • 检查我的编辑,您的 xaml 在 xmlns:i 部分中错误地引用了它
    • @WymyslonyNick 这为你解决了吗?
    • 不工作。 1 个错误:错误 1 ​​无法将“EventTrigger”类型的实例添加到“TriggerCollection”类型的集合中。只允许使用“T”类型的项目。
    • 看我编辑的答案。不需要对 Microsoft.Expression.Interactions 的任何引用。我用了你原来的 i: 参考,事实上,任何一个都有效。我不知道你是在使用 RelayCommand 还是什么,没关系。我没有看到有关 EventTrigger 的任何错误,所以你一定有问题。
    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多