Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 什么是「;“适当的”;WPF MVVM中如何将frameworkElement事件绑定到viewmodel处理程序?_C#_.net_Wpf_Events_Mvvm - Fatal编程技术网

C# 什么是「;“适当的”;WPF MVVM中如何将frameworkElement事件绑定到viewmodel处理程序?

C# 什么是「;“适当的”;WPF MVVM中如何将frameworkElement事件绑定到viewmodel处理程序?,c#,.net,wpf,events,mvvm,C#,.net,Wpf,Events,Mvvm,这就是我的困境,我想在我的视图模型上处理视图事件,问题是,为了添加事件处理程序,我的视图必须有一个代码隐藏文件,因此必须设置一个class属性。我99%肯定这是个坏主意,说实话,我甚至不知道该怎么做(除了明显的x:Class=”“部分),在MVVM应用程序中,什么是正确的方法 <ResourceDictionary> <DataTemplate DataType="{x:Type vm:OutletViewModel}"> <Button C

这就是我的困境,我想在我的视图模型上处理视图事件,问题是,为了添加事件处理程序,我的视图必须有一个代码隐藏文件,因此必须设置一个class属性。我99%肯定这是个坏主意,说实话,我甚至不知道该怎么做(除了明显的x:Class=”“部分),在MVVM应用程序中,什么是正确的方法

<ResourceDictionary>
    <DataTemplate DataType="{x:Type vm:OutletViewModel}">
        <Button Click="IHaveNoBinding">
    </DataTemplate>
</ResourceDictionary>

使用:


有关可在视图模型中使用的有用命令实现,请参见我的

假设您不能使用命令,请使用。

使用:


有关可在视图模型中使用的有用命令实现,请参见我的


假设您不能使用命令,请使用。

我使用附加行为。附加行为基本上将事件转换为命令。查看此链接以获取示例:

下面是TextChangedBehavior的代码

 public static class TextChangedBehavior
  {
    public static readonly DependencyProperty TextChangedCommandProperty =
        DependencyProperty.RegisterAttached("TextChangedCommand",
                                            typeof(ICommand),
                                            typeof(TextChangedBehavior),
                                            new PropertyMetadata(null, TextChangedCommandChanged));

    public static ICommand GetTextChangedCommand(DependencyObject obj)
    {
      return (ICommand)obj.GetValue(TextChangedCommandProperty);
    }

    public static void SetTextChangedCommand(DependencyObject obj, ICommand value)
    {
      obj.SetValue(TextChangedCommandProperty, value);
    }

    private static void TextChangedCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
      TextBoxBase textBox = obj as TextBoxBase;

      if (textBox != null)
      {
        textBox.TextChanged += new TextChangedEventHandler(HandleTextChanged);
      }
    }

    private static void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
      TextBox textBox = sender as TextBox;
      if (textBox != null)
      {
        ICommand command = GetTextChangedCommand(textBox);
        command.Execute(textBox.Text);
      }
    }
  }
XAML:


我使用附加行为。附加行为基本上将事件转换为命令。查看此链接以获取示例:

下面是TextChangedBehavior的代码

 public static class TextChangedBehavior
  {
    public static readonly DependencyProperty TextChangedCommandProperty =
        DependencyProperty.RegisterAttached("TextChangedCommand",
                                            typeof(ICommand),
                                            typeof(TextChangedBehavior),
                                            new PropertyMetadata(null, TextChangedCommandChanged));

    public static ICommand GetTextChangedCommand(DependencyObject obj)
    {
      return (ICommand)obj.GetValue(TextChangedCommandProperty);
    }

    public static void SetTextChangedCommand(DependencyObject obj, ICommand value)
    {
      obj.SetValue(TextChangedCommandProperty, value);
    }

    private static void TextChangedCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
      TextBoxBase textBox = obj as TextBoxBase;

      if (textBox != null)
      {
        textBox.TextChanged += new TextChangedEventHandler(HandleTextChanged);
      }
    }

    private static void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
      TextBox textBox = sender as TextBox;
      if (textBox != null)
      {
        ICommand command = GetTextChangedCommand(textBox);
        command.Execute(textBox.Text);
      }
    }
  }
XAML:


一般来说,我不会将附加的行为模式用于像这样的简单事情。作为一名顾问,我发现这会使新开发人员的工作变得复杂

那么,当没有可用的命令时,如何处理控件交互呢。准备好把你自己从地板上抬起来:-)我会经常使用后面的代码来实现这一点。代码隐藏中的事件处理程序处理事件,它从事件参数收集所需的任何数据,然后将请求转发给视图模型。这样做不会有太大损失,因为大多数不支持ICommand的东西无论如何都无法利用隐藏/显示/启用/禁用


然而,有一些规则。代码隐藏只能用于将控件转发到视图模型。只要不将事件参数直接传递给视图模型,我认为以这种方式使用事件是可以的。事实上,在大规模应用程序中,代码总是落后的。如果您按照预期使用它们,即页面控件,我认为这样做没有坏处。

一般来说,我不会将附加的行为模式用于像这样简单的事情。作为一名顾问,我发现这会使新开发人员的工作变得复杂

那么,当没有可用的命令时,如何处理控件交互呢。准备好把你自己从地板上抬起来:-)我会经常使用后面的代码来实现这一点。代码隐藏中的事件处理程序处理事件,它从事件参数收集所需的任何数据,然后将请求转发给视图模型。这样做不会有太大损失,因为大多数不支持ICommand的东西无论如何都无法利用隐藏/显示/启用/禁用


然而,有一些规则。代码隐藏只能用于将控件转发到视图模型。只要不将事件参数直接传递给视图模型,我认为以这种方式使用事件是可以的。事实上,在大规模应用程序中,代码总是落后的。如果您按照预期使用它们,即页面控件,我认为这样做没有坏处。

代码隐藏一点也不坏。在很多情况下,您不能使用WPF数据绑定(例如PasswordBox),然后必须创建代码隐藏文件

本项目的ViewModel示例中显示了如何在不绑定的情况下使用密码箱:

WPF应用程序框架(WAF)


代码隐藏一点也不坏。在很多情况下,您不能使用WPF数据绑定(例如PasswordBox),然后必须创建代码隐藏文件

本项目的ViewModel示例中显示了如何在不绑定的情况下使用密码箱:

WPF应用程序框架(WAF)


好的,假设我不能使用命令,假设它是Textbox.textchangedfortext,使用绑定:好的,假设我不能使用命令,假设它是Textbox.textchangedfortext,使用绑定:
<TextBox behavior:TextChangedBehavior.TextChangedCommand="{Binding TextChangedCommand}" />