从数据源拖放到 WPF 窗口不起作用 [英] Drag and drop from datasource to WPF window not working

查看:18
本文介绍了从数据源拖放到 WPF 窗口不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为我的公司设计一个联系人管理程序.我们有 VS 2012,因为在我想用它来开发这个应用程序之前我从未使用过 WPF.

I have been tasked to design a contact management program for my company. We have VS 2012 and since I have never used WPF before I thought I would use it to develop this application.

在使用数据库的实体框架(顺便说一句首先是数据库)时,我在开始绑定时遇到了很大的问题.

I am having huge problem getting started on the binding when utilizing the entity framework for the database which btw is database first.

我已按照此链接中的说明操作这封信.http://msdn.microsoft.com/en-us/data/jj574514.aspx

I have followed the instructions within this link to the letter. http://msdn.microsoft.com/en-us/data/jj574514.aspx

对象显示在数据源窗口中就好了.但是当我拖放到我的窗口时,没有任何反应.不知道我做错了什么,也找不到其他有这个问题的人.

The objects show up in the data sources window just fine. But when I drag and drop to my window, nothing happens. No idea what I am doing wrong and cannot find anyone else with this problem.

有人可以帮我吗?我到处找.任何帮助表示赞赏

Can anyone help me out here? I have looked everywhere. Any help is appreciated

推荐答案

好的.我实际上翻阅了那篇文章,只是为了表现出诚意,让您知道我真的很想帮助您.

Ok. I actually went thru that article, just to show good faith and let you know that I actually want to help you.

我得出以下结论:

  • 那篇文章展示了从实体框架上下文获取数据并将其显示在 WPF DataGrid 中的一个非常基本的场景.
  • 它没有任何类型的验证或业务规则.
  • 它没有任何 UI 行为,例如有条件地启用/禁用或显示/隐藏任何 UI 元素.
  • 这种情况是 Designer 的用处,当您实际上不需要任何东西时,除了从/向数据库获取/保存数据之外.
  • 不幸的是(或者对我们所有以此为生的开发者来说是幸运的),大多数应用程序都需要一定程度的验证和业务规则以及一定程度的 UI 逻辑.
  • 在开发复杂的逻辑时,设计师真的没用.

可以在不需要复杂逻辑的情况下使用设计器,但是我必须警告您以下缺点:

You can use the designer for such situations where you don't require complex logic, however I must warn you the following cons:

  • Visual Studio WPF 设计器生成固定大小、固定位置的 UI.在具有不同屏幕分辨率和 DPI 设置的计算机上执行时,这些类型的 UI 效果不佳.就像 winform 一样.
  • 它还产生了包含许多不必要的东西(例如 x:Name="categoryIdColumn" 和诸如 Margin="13,13,43,191" 之类的东西的 XAML,它们是从可维护性/可扩展性的角度来看真的很糟糕)
  • 据我所知,设计器生成的 XAML 还包含一个 CollectionViewSource,这既是好事也是坏事.这是一件好事,因为它使 Design-DataGrid 中的时间数据,但它也很糟糕,因为它会用大量不需要的东西使您的 XAML 膨胀,并引入不必要的 使事情复杂化.
  • The Visual Studio WPF designer produces fixed-size, fixed-position UIs. these type of UIs don't work well when executed in computers with different screen resolutions and DPI settings. Just like winforms.
  • It also produces XAML that has many unnecessary things (such as x:Name="categoryIdColumn" and things like Margin="13,13,43,191" which are really bad from a maintainabilty / scalability point of view)
  • From what I've seen, the designer-generated XAML also contains a CollectionViewSource, this is both a good thing and a bad thing. It's a good thing because it enables Design-Time Data in the DataGrid, but it is also bad because it bloats your XAML with lots of unneeded things and introduces unnecessary <Window.Resources> that complicate things up.

现在,这是该 DataGrid 所需的非常少的 XAML,没有设计时数据支持:

Now, this is the very minimal XAML needed for that DataGrid, without Design-time data support:

<Window x:Class="MiscSamples.DesignTimeDataGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DesignTimeDataGrid">
    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGridTextColumn Header="Category Id" Binding="{Binding CategoryId}"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
    </DataGrid>
</Window>

你看到了吗?实际上,键入该内容(在 Intellisense 的帮助下更是如此)比浏览属性窗口并手动设置这些属性要快.

You see? It's actually faster to type that (much more so with the help of Intellisense) than what it takes for you to browse the property window and set these properties manually.

我的建议是你要熟悉 XAML,而不是硬着头皮做事

要记住的另一个非常重要的方面是,一般来说,您不会在 WPF 中的代码隐藏中放置任何内容 因为不需要,因此该教程实际上违背了 WPF 做事方式,但没关系,因为它实际上是实体框架教程,而不是 WPF 教程.

Another very important aspect to keep in mind is that generally speaking, you don't put anything in code-behind in WPF because it's not needed, therefore that tutorial is actually going against the WPF Way of doing things, but that's Ok because it's actually an Entity Framework tutorial, not a WPF tutorial.

易于开发

您确实需要重新考虑所谓的易于开发".说到 UI 开发,我称之为易于开发"实际上能够用 UI 做我想做的事不必求助于涉及 P/Invoke(无论这意味着什么)和所有者绘制"类型的东西的糟糕程序代码实践.

You really need to reconsider what you call "ease of development". When it comes to UI development, I call "ease of development" to actually being able to do what I want with the UI without having to resort to shitty procedural code practices involving P/Invoke (whatever that means) and "owner draw" type of things for evertyhing.

WPF 提供了真正的开发简易性,而不是 winforms 所展示的虚假的开发简易性

WPF provides REAL ease of development as opposed to the fake ease of development exhibited by winforms

  • winforms 让你可以和设计师一起做所有事情(这只是因为设计师生成的代码实际上太糟糕了,如果没有设计师,没有人会用过 winforms)但是当它出现的时候要添加复杂的数据绑定或 UI 逻辑,您将永远被 winform 的无能所困.

  • winforms lets you do everything with a designer (and this is just because the code the designer generates is actually so shitty that nobody would have ever used winforms if they didn't have the designer) but then when it comes to adding complex DataBinding or UI logic you're stuck with winforms' incapabilities forever.

WPF 鼓励手动编写 XAML,这不仅是因为 XAML 是声明性的(与程序化的 winforms 方法相反),还因为可定制性和可重用性的级别如此之高,以至于以设计者为中心的方法没有意义.

WPF encourages manual XAML writing, not only because XAML is declarative (as opposed to the procedural winforms approach), but also because the levels of customizability and reusability are so high that a designer-centric approach does not make sense.

拖放是最简单的方法

不,不是.这实际上是一条艰难的道路.简单的方法是学习 XAML 并能够做到 用 Winforms 做你无法想象的事情.

No it's not. It's actually the hard way. The easy way is to learn XAML and be able to do Things you can't even imagine to do with winforms.

如果以设计师为中心的方法对您来说仍然有意义,您可能想尝试 Expression Blend

If a designer-centric approach still makes sense to you, you may want to try Expression Blend

这篇关于从数据源拖放到 WPF 窗口不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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