在 WPF 中创建一个漂亮的 GUI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2135092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating a nice GUI in WPF
提问by Ram
I need to create a desktop CAD application which essentially should have a nice modern GUI. I am thinking of creating a WPF application so that I can have a rich user interface. Could some one suggest me a well designed desktop application GUI framework in WPF, please? I found some cool GUI in this video http://channel9.msdn.com/posts/Psychlist1972/Pete-at-PDC09-WPF-3d-Awesomeness-with-Tor-and-Robby/but not sure of the controls they used in their application. Does any one have an idea which controls did they use there?
我需要创建一个桌面 CAD 应用程序,它本质上应该有一个漂亮的现代 GUI。我正在考虑创建一个 WPF 应用程序,以便我可以拥有一个丰富的用户界面。有人可以建议我在 WPF 中设计一个精心设计的桌面应用程序 GUI 框架吗?我在这个视频中发现了一些很酷的 GUI http://channel9.msdn.com/posts/Psychlist1972/Pete-at-PDC09-WPF-3d-Awesomeness-with-Tor-and-Robby/但不确定他们使用的控件在他们的申请中。有没有人知道他们在那里使用了哪些控件?
Is there any property grid control in WPF? I tried to use the grid in Windows Forms. Customizing this grid to suit my requirement seems to be difficult. It shows all the properties of the object straight from the very base class to the most derived.
WPF中是否有任何属性网格控件?我尝试在 Windows 窗体中使用网格。自定义此网格以满足我的要求似乎很困难。它直接显示对象的所有属性,从最基类到最派生类。
回答by codekaizen
With WPF, a lot is possible. You'll find a wide variety of looks to various applications due to the fact that, unlike Windows Forms, WPF can be templated and styled much like HTML. Actual designers can easily bring a look and feel which is very difficult to accomplish in Windows Forms. Naturally, since it is so flexible, the look of highly styled applications will vary a great deal from application to application.
使用 WPF,很多事情都是可能的。您会发现各种应用程序的外观多种多样,因为与 Windows 窗体不同,WPF 可以像 HTML 一样进行模板化和样式化。实际设计人员可以轻松带来在 Windows 窗体中很难实现的外观和感觉。自然,由于它非常灵活,高度风格化的应用程序的外观会因应用程序而异。
That said, there are some very good 3rd party controls. All the usual suspects have control libraries for WPF: Telerik, Infragistics, ComponentOne, Actipro, Devxpressjust to name a few. Specifically, Actipro's Property Gridis very nice. There is also an open source onewhich I haven't evaluated, so can't speak to. WPF can also be "themed" by applying pre-compiled styles to controls. There are example themes found here: http://wpfthemes.codeplex.com/.
也就是说,有一些非常好的 3rd 方控件。所有常见的犯罪嫌疑人对WPF控件库:Telerik的,Infragistics的,ComponentOne的,Actipro,Devxpress只是仅举几例。具体来说,Actipro 的 Property Grid非常好。还有一个我没有评估过的开源的,所以不能说。WPF 还可以通过将预编译样式应用于控件来“主题化”。在这里可以找到示例主题:http: //wpfthemes.codeplex.com/。
Finally, WPF's strengths are not fully realized until you learn how to separate the view which gets drawn and managed by WPF and the logical abstraction of the view, called the view model. Josh Smith has a great article about this pattern, known as Model-View-ViewModel, here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx.
最后,直到您学习如何将 WPF 绘制和管理的视图与视图的逻辑抽象(称为视图模型)分开后,WPF 的优势才能完全实现。Josh Smith 有一篇关于这种模式的很棒的文章,称为 Model-View-ViewModel,在这里:http: //msdn.microsoft.com/en-us/magazine/dd419663.aspx。
回答by Ray Burns
I think Microsoft saw no point in including a PropertyGrid control in WPF because it is so trivial to create your own, and if they created the control it would be harder to style.
我认为 Microsoft 认为在 WPF 中包含 PropertyGrid 控件毫无意义,因为创建自己的控件非常简单,如果他们创建了控件,则样式会更难。
To create your own PropertyGrid, just use a <ListBox>
with an <ItemsTemplate>
that has a <DockPanel>
containing a <TextBlock>
docked to the left for the property name and a <ContentPresenter>
for the value editor, then enable grouping on the Category
property.
要创建您自己的 PropertyGrid,只需使用 a<ListBox>
和<ItemsTemplate>
,其中<DockPanel>
包含<TextBlock>
停靠在左侧的属性名称和<ContentPresenter>
值编辑器,然后在Category
属性上启用分组。
The only code you need to write is the code that reflects on the object and creates the list of properties.
您需要编写的唯一代码是反映对象并创建属性列表的代码。
Here is a rough idea of what you would use:
以下是您将使用的内容的粗略想法:
DataContext =
from pi in object.GetType().GetProperties()
select new PropertyGridRow
{
Name = pi.Name,
Category = (
from attrib in pi.GetCustomAttributes(false).OfType<CategoryAttribute>()
select attrib.Category
).FirstOrDefault() ?? "None",
Description = (
from attrib in pi.GetCustomAttributes(false).OfType<DescriptionAttribute>()
select attrib.Description
).FirstOrDefault(),
Editor = CreateEditor(pi),
Object = object,
};
The CreateEditor method would simply construct an appropriate editor for the property with a binding to the actual property value.
CreateEditor 方法将简单地为该属性构造一个适当的编辑器,并绑定到实际的属性值。
In the XAML, the <ListBox.ItemTemplate>
would be something like this:
在 XAML 中,<ListBox.ItemTemplate>
将是这样的:
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding PropertyName}" Width="200" />
<ContentPresenter DataContext="{Binding Object}" Content="{Binding Editor}" />
</DockPanel>
</DataTemplate>
I'll let you fill in the rest of the details.
我会让你填写其余的细节。