如何在WPF MVVM中向文本框显示数据库值? [英] How to display database value to a textbox in WPF MVVM?
本文介绍了如何在WPF MVVM中向文本框显示数据库值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要与数据库值绑定的文本框
就像,我有一个文本框,一个组合框和一个按钮.Mr X从组合框中选择一个值(比如Employee No)点击按钮。根据价值(比如员工编号),相关名称应填写文本框。如何实现?
我尝试过:
I have a textbox which need to bind with database value
Like,i have a text box a combo box and a button.Mr X selects a value(say Employee No) from combo box and click the button.Based on the value(say Employee No) the associated name should fill in the text box.How can i achieve?
What I have tried:
<TextBox Template="{StaticResource TextBoxBaseControlTemplate}" x:Name="txttitle1" HorizontalAlignment="Left" Height="30" Margin="578,22,0,0" TextWrapping="Wrap" Text="{Binding PODetail.PO_TITLE1,Mode=TwoWay}" VerticalAlignment="Top" Width="152"/>
Below is the get/set of my model class
public PurchaseOrderModel podetail = new PurchaseOrderModel();
public PurchaseOrderModel PODetail
{
get { return podetail; }
set { podetail = value; OnPropertyChanged("PODetail"); }
}
PODetail.PO_TITLE1 = pom.PO_TITLE1;-->In this the value sets to text box in viewmodel but not reflecting to View
推荐答案
我相信您的问题是在绑定后设置PO_TITLE1的值并设置了类PODetail。您必须在PurchaseOrderModel类中实现INotifyPropertyChanged,并在PO_TITLE1值更改时触发PropertyChanged事件。
I believe your problem is that you set the value of PO_TITLE1 after the binding and the class PODetail has been set. You will have to implement the INotifyPropertyChanged in the PurchaseOrderModel class and trigger the PropertyChanged Event when the PO_TITLE1 value is changed.
这篇关于如何在WPF MVVM中向文本框显示数据库值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文