简体   繁体   English

在 WPF 中工作时,我应该使用 System.Windows.Controls 还是 System.Windows.Z645024Z662531918A3CAECAC62531918292ACAE62531918A3CAECAC

[英]When working in WPF, should I be using controls from System.Windows.Controls or System.Windows.Forms?

So I am starting to use XAML to create WPF applications in Visual Studio, and it seems a LOT of the controls are different.所以我开始使用 XAML 在 Visual Studio 中创建 WPF 应用程序,似乎很多控件都不同。 Same name, but different.同名,但不同。

Most notably (for me) is the Treeview and RichTextBox.最值得注意的是(对我来说)是 Treeview 和 RichTextBox。

Since I am working in WPF, should I use System.Windows.Forms.TreeView or System.Windows.Controls.TreeView (same for rich text boxes, etc.)? Since I am working in WPF, should I use System.Windows.Forms.TreeView or System.Windows.Controls.TreeView (same for rich text boxes, etc.)?

I do not like the way that the controls in System.Windows.Controls work.我不喜欢System.Windows.Controls中的控件的工作方式。 It may be because I am used to them in Forms, but for example, I don't get why Controls.RichTextBox does not have a Text or a Clear method.可能是因为我在 Forms 中习惯了它们,但例如,我不明白为什么Controls.RichTextBox没有 Text 或 Clear 方法。

At the same time, it is really annoying having to use <wf:TreeView /> notation a lot, so I try to use the Sys.Windows.Controls versions of stuff as much as I can.同时,不得不经常使用 <wf:TreeView /> 符号真的很烦人,所以我尽量使用Sys.Windows.Controls版本的东西。

Is one way better than the other?一种方式比另一种更好吗? Or is it just preference?还是只是偏好?

The preferred way is of course using WPF-native System.Windows.Controls .首选方法当然是使用 WPF 原生System.Windows.Controls I think you'll get used to them.我想你会习惯他们的。 WPF controls are superior to Windows Forms ones due to their support of binding, animation, styling, etc. WPF 控件优于 Windows Forms 控件,因为它们支持绑定、Z6F1C25ED1523962FZBEBE9EED 等。

The WPF controls are feature-rich, but the features are sometimes implemented not the same way as the Windows Forms counterparts. WPF 控件功能丰富,但有时实现这些功能的方式与 Windows Forms 对应的方式不同。 In particular, for RichTextBox you can use RichTextBox.Document for working with the text inside.特别是,对于RichTextBox ,您可以使用RichTextBox.Document来处理里面的文本。

For example, getting the text from a RichTextBox can be done as follows:例如,可以按如下方式从RichTextBox获取文本:

TextRange textRange = new TextRange(richTextBox.Document.ContentStart,
                                    richTextBox.Document.ContentEnd);
string text = textRange.Text;

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

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