在 ShowDialog (WPF MVVM) 中将 UpdateSourceTrigger 设置为显式

发布于 2024-12-08 09:35:25 字数 426 浏览 0 评论 0原文

在示例中 看到了这个示例 - Binding.UpdateSourceTrigger Property UpdateSourceTrigger 设置为 Explicit,然后在视图代码中调用 TextBox 名称的 UpdateSource。

但是,如果我使用 MVVM dp,我不想为控件命名,并且源属性位于 VM 中而不是视图中,那么将控件绑定到 VM 属性并将 UpdateSourceTrigger 设置为显式的正确方法是什么?

我想这样做是因为在我的例子中它是 ShowDialog 窗口,并且我希望仅当用户单击“确定”时源才会更新

提前致谢!

I saw this example - Binding.UpdateSourceTrigger Property

in the example the UpdateSourceTrigger set to Explicit and then in the view code he call to UpdateSource of the TextBox name.

But if i use the MVVM dp i dont want to have names to my controls and source properties are in the VM and not in the view so what is the right way to bind controls to VM properties and set the UpdateSourceTrigger to explicit?

I want to do this because in my case its ShowDialog window and I want that the source will update only if the user click "ok"

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-12-15 09:35:25

如果您真正使用 MVVM,那么您的“确定”按钮单击必须由某些命令处理。此命令必须来自您的 ViewModel显式绑定的属性必须再次来自您的ViewModel。那么是什么阻止了你呢。

  1. 不要使用 Explicit 绑定,而是使用 OneWay 绑定。
  2. 在按钮中,绑定命令并将命令参数绑定到 OneWay 绑定的依赖项属性。
  3. 在命令的执行处理程序(必须是 ViewModel 中的某个方法)中,使用传入的参数更改 ViewModel 的属性。
  4. 从您的 ViewModel 中引发该属性的 NotifyPropertyChanged

例如,

假设我需要在单击“确定”按钮时将文本框的文本更新回我的模型。

因此,我有一个 EmployeeViewModel 类,其中包含 EmployeeName 属性。该属性有一个 getter 和一个 setter。 setter 会引发属性更改通知。视图模型还有另一个名为 SaveNameCommandICommand 类型属性,它返回一个命令供我执行。

EmployeeViewModel 是我的视图的数据上下文类型。 Myview 有一个绑定到 EmployeeNameTextBox (名为 x:Name="EmployeeNameTxBx")OneWay 和一个按钮 OK< /代码>。我将 Button.Command 属性绑定到 EmployeeViewModel.SaveNameCommand 属性,并将 Button.CommandParameter 绑定到 EmployeeNameTxBx.Text 属性。

      <StackPanel>
          <TextBox x:Name="EmployeeNameTxBx"
                   Text="{Binding EmployeeName, Mode=OneWay}" />
          <Button Content="OK"
                  Command="{Binding SaveNameCommand}"
                  CommandParameter="{Bidning Text, ElementName=EmployeeNameTxBx}" />
      </StackPanel>

在我的 EmployeeViewModel 中,我有 OnSaveNameCommandExecute(object param) 方法来执行我的 SaveNameCommand

在此执行此代码...

     var text = (string)param;
     this.EmployeeName = text;

这样,只需单击“确定”按钮,即可将 TextBox 的文本更新回模型的 EmployeeName 属性。

编辑

查看下面的评论,我发现您正在尝试在 UI 上实现验证。现在这改变了一些事情。

仅当您的输入控件(例如文本框)是 TwoWay 绑定时,IDataErrorInfo 和相关验证才有效。是的,这就是它的意图。所以现在你可能会问“如果我们使用 IDataErrorInfo,这是否意味着不允许无效数据传递到模型的整个概念在 MVVM 中是徒劳的”?

其实不是!

请参阅 MVVM 不强制执行仅应返回有效数据的规则。它接受无效数据,这就是 IDataErrorInfo 的工作原理并引发错误通知。关键是 ViewModel 只是 View 的软拷贝,因此它可能。它应该确保这种肮脏不会提交到您的外部接口,例如服务或数据库。

ViewModel 应该通过测试无效数据来限制此类无效数据流。如果我们启用了 TwoWay 绑定,这些数据就会出现。因此,考虑到您正在实现 IDataErrorInfo,那么您需要拥有 MVVM 中完全允许的 TwoWay 绑定。

方法 1:

如果我想在单击按钮时显式验证 UI 上的某些项目该怎么办?

为此,请使用延迟验证技巧。在您的 ViewModel 中有一个名为 isValidating 的标志。默认设置为 false。

在您的 IDataErrorInfo.this 属性中,通过检查 isValidating 标志来跳过验证...

    string IDataErrorInfo.this[string columnName]
    {
      get
      {
        if (!isValidating) return string.Empty;

        string result = string.Empty;
        bool value = false;

        if (columnName == "EmployeeName")
        {
            if (string.IsNullOrEmpty(AccountType))
            {
                result = "EmployeeName cannot be empty!";
                value = true;
            }
        }
        return result;
      }
    }

然后在您的 OK 命令执行处理程序中,检查员工姓名,然后引发同一属性的属性更改通知事件...

    private void OnSaveNameCommandExecute(object param)
    {
         isValidating = true;
         this.NotifyPropertyChanged("EmployeeName");
         isValidating = false;
    }

这将触发仅当您单击“确定”时才进行验证。请记住,EmployeeName 必须包含无效数据才能进行验证。

方法 2:

如果我想在 MVVM 中显式更新没有 TwoWay 模式的绑定该怎么办?

那么您将必须使用附加行为。该行为将附加到“确定”按钮,并将接受需要刷新其绑定的所有项目的列表。

       <Button Content="OK">
           <local:SpecialBindingBehavior.DependentControls>
                <MultiBinding Converter="{StaticResource ListMaker}">
                    <Binding ElementName="EmployeeNameTxBx" />
                    <Binding ElementName="EmployeeSalaryTxBx" />
                    ....
                <MultiBinding>
           </local:SpecialBindingBehavior.DependentControls>
       </Button>

ListMaker 是一个 IMultiValueConverter,它只是将值转换为列表...

       Convert(object[] values, ...)
       {
            return values.ToList();
       }

在您的 SpecialBindingBehavior 中有一个 DependentControls 属性更改了处理程序...

      private static void OnDependentControlsChanged(
          DependencyObject depObj,
          DependencyPropertyChangedEventArgs e) 
      {
           var button = sender as Button;
           if (button != null && e.NewValue is IList)
           {
               button.Click 
                    += new RoutedEventHandler(
                         (object s, RoutedEventArgs args) =>
                         {
                              foreach(var element in (IList)e.NewValue)
                              {
                                 var bndExp
                                   = ((TextBox)element).GetBindingExpression(
                                       ((TextBox)element).Textproperty);

                                 bndExp.UpdateSource();
                              }
                         });
           }
      }

但我仍然建议您使用我之前的基于纯 MVVM **方法 1

If you are using MVVM truely then your OK button click must be handled by some Command. This command must be coming from your ViewModel. The Expliticly bound properties must be coming from your ViewModel again. So whats stopping you.

  1. Do not use Explicit binding but use OneWay binding.
  2. In you button, bind a command and bind a command parameter to the OneWay bound Dependency property.
  3. In your Command's Execute handler (which must be some method from your ViewModel), change the ViewModel's property with the parameter coming.
  4. Raise the NotifyPropertyChanged for that property from your ViewModel.

E.g.

Assume I need to update a TextBox's Text back into my model on OK button click.

So for that I have a EmployeeViewModel class that has EmployeeName property in it. The property is has a getter and a setter. The setter raises property changed notification. The view model also has another property of type ICommand named SaveNameCommand that return a command for me to execute.

EmployeeViewModel is the data context type of my view. Myview has a TextBox (named as x:Name="EmployeeNameTxBx") OneWay bound to the EmployeeName and a Button as OK. I bind Button.Command property to EmployeeViewModel.SaveNameCommand property and Button.CommandParameter is bound to EmployeeNameTxBx.Text property.

      <StackPanel>
          <TextBox x:Name="EmployeeNameTxBx"
                   Text="{Binding EmployeeName, Mode=OneWay}" />
          <Button Content="OK"
                  Command="{Binding SaveNameCommand}"
                  CommandParameter="{Bidning Text, ElementName=EmployeeNameTxBx}" />
      </StackPanel>

Inside my EmployeeViewModel I have OnSaveNameCommandExecute(object param) method to execute my SaveNameCommand.

In this perform this code...

     var text = (string)param;
     this.EmployeeName = text;

This way ONLY OK button click, updates the TextBox's text back into EmployeeName property of the model.

EDIT

Looking at your comments below, I see that you are trying to implement Validation on a UI. Now this changes things a little bit.

IDataErrorInfo and related validation works ONLY IF your input controls (such as TextBoxes) are TwoWay bound. Yes thats how it is intended. So now you may ask "Does this mean the whole concept of NOT ALLOWING invalid data to pass to model is futile in MVVM if we use IDataErrorInfo"?

Not actually!

See MVVM does not enforce a rule that ONLY valid data should come back. It accept invalid data and that is how IDataErrorInfo works and raises error notfications. The point is ViewModel is a mere softcopy of your View so it can be dirty. What it should make sure is that this dirtiness is not committed to your external interfaces such as services or data base.

Such invalid data flow should be restricted by the ViewModel by testing the invalid data. And that data will come if we have TwoWay binding enabled. So considering that you are implementing IDataErrorInfo then you need to have TwoWay bindings which is perfectly allowed in MVVM.

Approach 1:

What if I wan to explicitly validate certain items on the UI on button click?

For this use a delayed validation trick. In your ViewModel have a flag called isValidating. Set it false by default.

In your IDataErrorInfo.this property skip the validation by checking isValidating flag...

    string IDataErrorInfo.this[string columnName]
    {
      get
      {
        if (!isValidating) return string.Empty;

        string result = string.Empty;
        bool value = false;

        if (columnName == "EmployeeName")
        {
            if (string.IsNullOrEmpty(AccountType))
            {
                result = "EmployeeName cannot be empty!";
                value = true;
            }
        }
        return result;
      }
    }

Then in your OK command executed handler, check employee name and then raise property change notification events for the same property ...

    private void OnSaveNameCommandExecute(object param)
    {
         isValidating = true;
         this.NotifyPropertyChanged("EmployeeName");
         isValidating = false;
    }

This triggers the validation ONLY when you click OK. Remember that EmployeeName will HAVE to contain invalid data for the validation to work.

Approach 2:

What if I want to explicitly update bindings without TwoWay mode in MVVM?

Then you will have to use Attached Behavior. The behavior will attach to the OK button and will accept list of all items that need their bindings refreshed.

       <Button Content="OK">
           <local:SpecialBindingBehavior.DependentControls>
                <MultiBinding Converter="{StaticResource ListMaker}">
                    <Binding ElementName="EmployeeNameTxBx" />
                    <Binding ElementName="EmployeeSalaryTxBx" />
                    ....
                <MultiBinding>
           </local:SpecialBindingBehavior.DependentControls>
       </Button>

The ListMaker is a IMultiValueConverter that simply converts values into a list...

       Convert(object[] values, ...)
       {
            return values.ToList();
       }

In your SpecialBindingBehavior have a DependentControls property changed handler...

      private static void OnDependentControlsChanged(
          DependencyObject depObj,
          DependencyPropertyChangedEventArgs e) 
      {
           var button = sender as Button;
           if (button != null && e.NewValue is IList)
           {
               button.Click 
                    += new RoutedEventHandler(
                         (object s, RoutedEventArgs args) =>
                         {
                              foreach(var element in (IList)e.NewValue)
                              {
                                 var bndExp
                                   = ((TextBox)element).GetBindingExpression(
                                       ((TextBox)element).Textproperty);

                                 bndExp.UpdateSource();
                              }
                         });
           }
      }

But I will still suggest you use my previous pure MVVM based **Approach 1.

半世晨晓 2024-12-15 09:35:25

这是一个老问题,但我仍然想为偶然发现这个问题的其他用户提供一种替代方法......
在我的视图模型中,我不会直接在 get/set Property 方法中公开模型属性。我对所有属性使用内部变量。然后我双向绑定所有属性。所以我可以像“通常”一样进行所有验证,因为只有内部变量发生了变化。在视图模型构造函数中,我将模型对象作为参数,并将内部变量设置为模型的值。现在,当我单击“保存”按钮(->“保存命令”在我的视图模型中触发)并且没有错误时,我将模型的所有属性设置为相应内部变量的值。如果我单击“取消/撤消”按钮(在视图模型中触发取消命令),我将内部变量设置为未触及模型的值(使用视图模型属性的设置器,以便 NotifyPropertyChanged被调用并且视图显示更改=旧值)。

另一种方法是在模型中实现 Memento-Support,因此在开始编辑之前,您可以在模型中调用一个函数来保存当前值,如果取消编辑,您可以调用一个函数来恢复这些值...这样您将在任何地方都有撤消/取消支持,而不仅仅是在一个视图模型中......
我已经在不同的项目中实现了这两种方法,并且都工作正常,这取决于项目的要求......

This is an old question but still I want to provide an alternative approach for other users who stumble upon this question...
In my viewmodels, I do not expose the model properties directly in the get/set Property methods. I use internal variables for all the properties. Then I bind all the properties two-way. So I can do all the validation as "usual" because only the internal variables are changed. In the view model constructor, I have the model object as parameter and I set the internal variables to the values of my model. Now when I click on the "Save" Button (-> Save Command fires in my view model fires) and there are no errors, I set all the properties of my model to the values of the correspondng internal variable. If I click on the "Canel/Undo"-Button (-> Cancel-Command in my view model fires), I set the internal variables to the values of my untouched model (using the setters of the view model properties so that NotifyPropertyChanged is called and the view shows the changes=old values).

Yet another approach would be to implement Memento-Support in the model, so before you start editing you call a function in the model to save the current values, and if you cancel editing you call a function to restore those values...that way you would have the undo/cancel support everywhere an not just in one view model...
I've implemented both methods in different projects and both work fine, it depends on the requirements of the project...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文