wpf中的数据绑定问题

时间:2013-11-27 21:32:07

标签: c# wpf

我是C#的新手。我想解决数据绑定问题。问题是:

我在XAML部分中为文本框定义了数据绑定,如下所示;

 <Window x:Class="WpfAppl2_DB_Entity.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:l="clr-namespace:WpfAppl2_DB_Entity"
         Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
     <Window.Resources>
        <l:Emp x:Key="myEmp"/>
     </Window.Resources>
     <Grid Name="grid1">
         <TextBox Height="18" HorizontalAlignment="Left" Margin="229,94,0,0"
                 Name="textBox1" VerticalAlignment="Top" Width="96"  >
             <TextBox.Text>
                 <Binding Source="{StaticResource myEmp}" Path="empno" Mode = "TwoWay">
                     <Binding.ValidationRules>
                         <ExceptionValidationRule/>
                     </Binding.ValidationRules>
                 </Binding>
             </TextBox.Text>
         </TextBox>    
         <!-- more text boxes come here -->
     </Grid>
</Window>

这里项目的主命名空间是“WpfAppl2_DB_Entity”,我给了前缀“l”,然后我定义了一个静态资源“myEmp”,它是“Emp”类的对象,Emp类也定义在同一个在同一名称空间中,这是一个映射到数据库中表“emp”的实体类。然后我将TextBox“textBox1”绑定到“myEmp”对象的“empno”属性。 (我希望到目前为止读者都清楚)。表单上还有4个文本框,它们与Emp的其他4个属性绑定。

我在主窗口上定义了一个按钮(content =“Find”,目的是在emp表中找到一个在texbox1中具有empno = value的记录)。在“查找”按钮的点击事件中,我写了这个:

            emps = amirDB.GetTable<Emp>();  // amirDB is an instance of a class  derived from DataContext class
            Emp qryEmp = new Emp();
            qryEmp = this.Resources["myEmp"] as Emp;
            var empQuery = from o in emps
                          where o.empno == Convert.ToInt32(textBox1.Text)
                         select o;
            foreach (Emp rec in empQuery)
            {
                qryEmp = new Emp();
                qryEmp.empno = rec.empno;
                qryEmp.ename = rec.ename;
                qryEmp.job = rec.job;
                qryEmp.sal = rec.sal;
                qryEmp.deptno = rec.deptno;
                break;   // we want to retrieve at most one record
            }

现在由于文本框绑定到静态资源“myEmp”的不同属性,我们创建了一个新的Emp对象“qryEmp”,然后将静态资源分配给这个新对象(qryEmp = this.Resources [“myEmp” “]作为Emp;)。因此,这意味着2个变量引用了内存中的同一个对象(对吗?),之后我将检索到的记录的不同属性分配给qryEmp对象的相应属性。所以qryEmp现在拥有从DB检索到的完整记录。由于myEmp也指向同一个对象,它也应该有记录。由于文本框绑定到myEmp对象,我猜测应该更新文本框以显示完整的记录数据(所有字段)。但是我看到文本框保持空白(只有第一个文本框具有键盘输入的值)。

我的问题是为什么文本框不显示检索到的记录值?

另一种方法是,我将rec。*字段值直接分配给上面foreach循环中的文本框(textBox2.Text = rec.ename,textBox3 = rec.job,....),在这种情况下文本框显示所有预期的值。

I wanted the code to assign front end/back end field values without referring to text boxes, because Microsoft claims that wpf separates the UI from business logic. So, my approach was to just bind all the text boxes to appropriate properties of an object, and then in programming logic, I use only the program objects (myEmp, qryEmp), no UI objects.

有人可以帮忙吗?让我知道我在这里做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您创建了太多Emp个实例。

替换

Emp qryEmp = new Emp();
qryEmp = this.Resources["myEmp"] as Emp;

通过

Emp qryEmp = this.Resources["myEmp"] as Emp;

foreach (Emp rec in empQuery)
{
    qryEmp = new Emp();
    qryEmp.empno = rec.empno;
    ...
}

通过

foreach (Emp rec in empQuery)
{
    qryEmp.empno = rec.empno;
    ...
}

确保

  • Emp实现了 INotifyPropertyChanged 接口,
  • empno等是Emp中的公共属性(不是字段) 类,
  • 并且这些属性会引发PropertyChanged事件。

您可能还想用

替换foreach (Emp rec in empQuery)循环
Emp rec in empQuery.FirstOrDefault();
if (rec != null)
{
    qryEmp.empno = rec.empno;
    ...
}