简单的CRUD使用WPF和数据绑定 [英] Simple CRUD work with WPF and data binding

查看:133
本文介绍了简单的CRUD使用WPF和数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Windows应用程序开发中使用WPF,只是想把它放在第一位。我正在使用Visual Studio 2010和.NET Framework 4.0。



我正在开发一个相当简单的管理工具。为了简单起见,让我说我正在使用Employee数据。我创建了一个名为UserDetail的UserControl,它具有与Employee类相关的所有字段。



在UserDetail上绑定输入控件的最简单方法是什么?创建新实例时可能会有一个空白屏幕,如果编辑现有实例,则可能会有一个空白屏幕。



我在构造函数中尝试过以下内容:

  DataContext = _employee; 

我在XAML中绑定了一个这样的控件:

 < TextBox Name =txtFirstNameText ={Binding FirstName}/> 

这是有效的,但它似乎不是正确的方法。这似乎是一个简单的例子,但我还没有找到任何有用的东西。



任何有关这种简单绑定的教程的建议或链接?

解决方案

像Joel说你在正确的轨道上。在某些时候,你需要在后面的代码中明确设置DataContext。这通常会在顶层完成,就像在Window类中一样 - 所有的DataContext应该通过DataBindings设置。



对于您的具体示例,您说您正在使用UserControl。您应该能够直接将UserDetails控件的DataContext绑定到控件正在使用的位置,因此您不需要在UserControl代码后面设置DataContext。至少通常UserControl的用户将会告诉UserControl要使用哪些数据。



假设您有一个具有SelectedEmployee属性的AllEmployees类。您将窗口中的代码后面的DataContext设置为此对象的实例。现在,您希望您的窗口显示SelectedEmployee的用户详细信息。 UserDetails控件是假定它有一个Employee对象绑定到它的DataContext,现在你确定它有一个通过在DataBinding中设置它:

 < Window xmlns:local =YourNamespaceHoldingTheUserDetailsControl> 
..
< local:UserDetails DataContext = {Binding SelectedEmployee} />
..
< ; / Window>

您还可以向您的UserDetails类添加一个Employee属性,该类将设置DataContext - 如果你认为这在绑定中看起来更好。



为了改善GUI和域模型之间的分离,你应该明确地了解 MVVM模式我最初从观看这个视频由Jason Dollinger几次,视频会教你很多我们关于WPF的事情。 这个问题在这个问题上似乎也有一些宝贵的链接。



祝你好运WPF!它与其他GUI框架有点不同,所以学习曲线可能有点陡峭。但是一旦你进入它,这是一个美好的地方!


I'm new to WPF for Windows application development, just wanted to put that out there first. I'm using Visual Studio 2010 and .NET Framework 4.0.

I'm working on a rather simple administration tool. For simplicity sake lets say I'm working with Employee data. I've created a little UserControl called UserDetail that has all the fields that relate to the Employee class.

What is the simplest way to bind the input controls on my UserDetail? This could have a blank screen when creating a new instance, or existing values if editing an existing instance.

I tried the following in the constructor:

DataContext = _employee;

And I bound a control like this in the XAML:

<TextBox Name="txtFirstName" Text="{Binding FirstName}"/>

This works, but it doesn't seem like the correct way to do it. This seems like a simple example, but I haven't found anything helpful yet.

Any suggestions or links to an tutorial for this sort of simple binding?

解决方案

Like Joel says you're on the right track. At some point you need to explicitly set the DataContext in the code behind. This will typically be done on a top level - like in your Window class - and all further DataContext's should be set through DataBindings.

For your specific example you say you're working on a UserControl. You should be able to bind the DataContext of the UserDetails control directly where the control is being used, and hence you shouldn't need to set the DataContext in the code behind for the UserControl. At least normally the user of your UserControl is who would tell the UserControl which data to use.

Let's say you have a class AllEmployees with a property SelectedEmployee. You set an instance of this object as DataContext on your Window - in the code behind. Now you want your window to show user details for the SelectedEmployee. The UserDetails control is built assuming it has an Employee object bound to its DataContext, and now you make sure it has one by setting this in a DataBinding:

<Window xmlns:local="YourNamespaceHoldingTheUserDetailsControl>
    ..
    <local:UserDetails DataContext={Binding SelectedEmployee} />
    ..
</Window>   

You can also add an Employee property to your UserDetails class which will set the DataContext - if you think this looks better in the binding.

To improve the separation between the GUI and your domain model you should definitly learn about the MVVM pattern. I initially learned from watching this video by Jason Dollinger a few times. The video will teach you a lot of useful things about WPF. This question also seems to have some valuable links on the subject.

Good luck learning WPF! It's a bit different from other GUI frameworks, so the learning curve might be a bit steep. But once you get into it it's a wonderful place to be!

这篇关于简单的CRUD使用WPF和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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