将UpdateSourceTrigger设置为ShowDialog中的显式(WPF MVVM) [英] Set UpdateSourceTrigger to Explicit in ShowDialog (WPF MVVM)

查看:212
本文介绍了将UpdateSourceTrigger设置为ShowDialog中的显式(WPF MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个例子 - Binding.UpdateSourceTrigger属性



在UpdateSourceTrigger设置为Explicit的示例中,然后在视图代码中调用UpdateSource的TextBox名称。



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



我想这样做,因为在我的情况下,它的ShowDialog窗口,我希望源将更新只有当用户点击好



提前感谢!

解决方案

如果您真正使用MVVM,那么您的OK按钮点击必须由一些 code>。此命令必须来自您的 ViewModel Explicitly 绑定属性必须再次从您的 ViewModel 中。那么什么阻止你。


  1. 不要使用显式绑定,但使用 OneWay 绑定

  2. 在你的按钮中,绑定命令并将命令参数绑定到 OneWay 绑定的依赖属性。

  3. 在命令的Execute处理程序(它必须是ViewModel中的某些方法)中,使用参数来更改ViewModel的属性。

  4. ViewModel 中提高该资源的 NotifyPropertyChanged

例如



假设我需要在OK按钮上更新一个TextBox的文本到我的模型。



因此,我有一个 EmployeeViewModel 类,其中包含 EmployeeName 属性。该物业有一个吸气剂和一个设置者。设置者提高属性更改通知。视图模型还具有另一个类型为 ICommand 的属性,命名为 SaveNameCommand ,该属性返回一个命令以供我执行。 >

EmployeeViewModel 是我的视图的数据上下文类型。 Myview有一个 TextBox (命名为x:Name =EmployeeNameTxBx) OneWay 绑定到 EmployeeName 和一个按钮作为 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;

这样只需点击OK按钮,将TextBox的文本更新回 EmployeeName 该模型的财产。



编辑



在下面的评论中,我看到您正在尝试在UI上实现验证。现在这一点改变了一点。



IDataErrorInfo 和相关验证功能只有当您的输入控件(如TextBoxes) TwoWay 绑定。是的,它是如何打算的所以现在你可以问这是否意味着不允许无效数据传递给模型的整个概念在MVVM中是徒劳的,如果我们使用IDataErrorInfo?



实际上不是! / p>

请参阅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不能空!
value = true;
}
}
返回结果;
}
}

然后在您的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 只需将值转换为列表...

 转换(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(varist in(IList)e.NewValue)
{
var bndExp
=((TextBox)元素).GetBindingExpression (
((TextBox)元素).Textproperty);

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

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


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!

解决方案

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.

这篇关于将UpdateSourceTrigger设置为ShowDialog中的显式(WPF MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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