【问题标题】:When working in WPF, should I be using controls from System.Windows.Controls or System.Windows.Forms?在 WPF 中工作时,我应该使用 System.Windows.Controls 还是 System.Windows.Forms 中的控件?
【发布时间】:2011-10-06 09:39:15
【问题描述】:

所以我开始使用 XAML 在 Visual Studio 中创建 WPF 应用程序,而且似乎很多控件都不同。同名,但不同。

最值得注意的是(对我而言)是 Treeview 和 RichTextBox。

由于我在 WPF 中工作,我应该使用 System.Windows.Forms.TreeView 还是 System.Windows.Controls.TreeView(对于富文本框等)?

我不喜欢System.Windows.Controls 中的控件的工作方式。可能是因为我习惯了 Forms 中的它们,但例如,我不明白为什么 Controls.RichTextBox 没有 Text 或 Clear 方法。

同时,不得不大量使用 表示法真的很烦人,所以我尽量使用Sys.Windows.Controls 版本的东西。

一种方式比另一种更好吗?还是只是偏好?

【问题讨论】:

    标签: wpf winforms forms xaml controls


    【解决方案1】:

    首选的方式当然是使用 WPF-native System.Windows.Controls。我想你会习惯他们的。 WPF 控件优于 Windows 窗体控件,因为它们支持绑定、动画、样式等。

    WPF 控件功能丰富,但有时实现这些功能的方式与 Windows 窗体对应项不同。特别是对于RichTextBox,您可以使用RichTextBox.Document 来处理其中的文本。

    例如,从RichTextBox 获取文本可以如下完成:

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

    【讨论】:

    • 哦。好的,这看起来不错(这正是我需要对 RTB 做的哈哈)。我会记住,功能可能就在那里,我可能只需要挖掘它!谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2017-07-01
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多