与WPF和数据绑定简单的CRUD工作 [英] Simple CRUD work with WPF and data binding

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

问题描述

我新的WPF的Windows应用程序开发,只想把他们赶出那里第一次。我使用的Visual Studio 2010和.NET Framework 4.0。

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.

我工作的一个相当简单的管理工具。为了简单起见,可以说,我与员工数据。我创建了一个用户控件稍微名为UserDetail拥有所有涉及到Employee类中的字段。

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.

什么是绑定在我的UserDetail输入控件最简单的方法?当创建一个新的实例,或者现有的值,如果编辑现有的实例,这可能会产生一个空白屏幕。

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.

我尝试了以下的构造:

DataContext = _employee;

和我约束这样的控制在XAML:

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?

推荐答案

像乔尔说,你是在正确的轨道。在某些时候,你需要明确设置的DataContext在code后面。在窗口的类象 - - 在顶层这通常被完成,所有进一步的DataContext的,应通过数据绑定设置。

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.

有关的具体的例子,你说你正在处理一个用户控件。您应该能够绑定的DataContext的的UserDetails直接控制,其中所使用的控制,所以你不应该需要设置的DataContext在背后为用户控件的code。至少有正常的用户控件的用户是谁可以告诉使用哪个数据的用户控件。

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.

假设你有一个类AllEmployees与属性SelectedEmployee。您可以设置此对象的DataContext你的窗口实例 - 在code后面。现在,你希望你的窗口来显示用户详细信息的SelectedEmployee。该的UserDetails控制是建立假设它拥有绑定到其的DataContext Employee对象,而现在你确保它通过在数据绑定设置该有一个:

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>   

您也可以将员工属性添加到您的UserDetails类,将设置的DataContext - 如果你认为这看起来装订好。

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.

要提高图形用户界面和领域模型之间的距离,你应该definitly了解 MVVM模式。我最初从看视频通过贾森多林格几次。视频会教你很多关于WPF有用的东西。也这个问题似乎对这个问题的一些有价值的链接。

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.

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

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!

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

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