在命令之前更新数据 [英] Updates the data before command

查看:49
本文介绍了在命令之前更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我的应用程序有点麻烦.我创建了一个产品经理"应用程序,用户可以在其中添加/更新/删除ListView显示的目录中的产品.我已经完成了更新过程,如下所示:
在我的ListView中,我将SelectedItem和SelectedIndex绑定到ViewModel中的两个属性.这些可以在"OneWayToSource"绑定模式下正常工作.

Greetings,

I have a bit messy problem with my application. I created a product "manager" application where the user has the ability to Add/Update/Remove products in a Catalog showed by a ListView. I''ve accomplished the Update procedure as follows:
In my ListView I''ve bound the SelectedItem and SelectedIndex to two Properties in the ViewModel. These are working correctly with "OneWayToSource" binding mode.

SelectedItem="{Binding Path=SelectedProduct, Mode=OneWayToSource}"



要编辑所选条目,我有一个TextBoxes,它显示了具有TwoWay绑定的SelectedProduct详细信息.



To Edit the selected entry I have TextBoxes which shows the SelectedProduct details with TwoWay binding.

<TextBox Grid.Row="0" Grid.Column="1" x:Name="txtName" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5,32,5,0" Text="{Binding Text, Path=SelectedProduct.Name, Mode=TwoWay}" />



然后这是我的问题:
每当我更改TextBox的内容时,模型都会自动更改,而不是在单击更新"按钮后才会更改.我做错了什么?



And then here is my problem:
Whenever I change the contents of the TextBox the model changes automatically and not after I click on the update button. What did I do wrong?

推荐答案

Text="{Binding Text, Path=SelectedProduct.Name, Mode=TwoWay}"


您在这里有问题,第二个文本"词实际上是路径,因此您必须删除第二个文本"或删除语句"Path = SelectedProduct.Name"

您可以在WPF中阅读有关数据绑定的更多信息,


you have something wrong here the second "text" word is actually the path, so you have to delete the second "text" or delete the statement "Path=SelectedProduct.Name"

you can read more about the data Binding in the WPF,


确定,您必须执行两个步骤

1-使您的SelectedProduct类继承自INotifyPropertyChanged并实现它,并在其上编写此方法
OK, you have to do two steps

1- make your SelectedProduct class inherit from INotifyPropertyChanged and implement it and write this method on it
public void OnPropertyChanged(String PropertyName) 
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
            }
        } 


使Name属性调用OnPropertyChanged("Name");
2-将
添加到您的绑定中


make the Name property call OnPropertyChanged("Name");
2- add to your binding this

Text="{Binding Path=SelectedProduct.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"


这篇关于在命令之前更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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