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. Same name, but different.
Most notably (for me) is the Treeview and 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.)?
I do not like the way that the controls in开发者_如何转开发 System.Windows.Controls
work. 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.
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.
Is one way better than the other? Or is it just preference?
The preferred way is of course using WPF-native 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.
The WPF controls are feature-rich, but the features are sometimes implemented not the same way as the Windows Forms counterparts. In particular, for RichTextBox
you can use RichTextBox.Document
for working with the text inside.
For example, getting the text from a RichTextBox
can be done as follows:
TextRange textRange = new TextRange(richTextBox.Document.ContentStart,
richTextBox.Document.ContentEnd);
string text = textRange.Text;
精彩评论