繁体   English   中英

WPF中的数据绑定问题

[英]Issue with Data Binding in 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”的实体类。 然后,将文本框“ 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现在具有从数据库检索的完整记录。 由于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.

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

您创建了太多的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;
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM