在 WPF 中,我可以通过向 ListBox
提供 ItemTemplate
来将任何 UI 添加到 ListBoxItem
:
In WPF, I can add whatever UI into ListBoxItem
s by providing the ListBox
with an ItemTemplate
:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Gray" CornerRadius="8" Padding="4,0,4,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Column="1" Content="Is Active Customer" IsChecked="{Binding IsActive}"/>
<Label Content="Id:" Grid.Row="1" HorizontalAlignment="Right"/>
<Label Content="Name:" Grid.Row="2" HorizontalAlignment="Right"/>
<TextBox Text="{Binding Id}" Grid.Row="1" Grid.Column="1"/>
<TextBox Text="{Binding Name}" Grid.Row="2" Grid.Column="1"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
结果:
有没有办法在 Windows 窗体中实现相同的功能?
Is there any way to achieve the same in Windows Forms?
1 - 有什么方法可以在 Windows 窗体中实现相同的功能,同时保持 View
和 Application Logic
关注点分离> 这样如果我以后想完全重新定义 View
,就不必重构整个应用程序了?
1 - Is there any way to achieve the same in Windows Forms, all while maintaining separation of concerns
between the View
and the Application Logic
in such a way that if I later wanted to completely redefine the View
, I wouldn't have to refactor the entire application?
2 - winforms 是否支持数据绑定,使得我的每个 ListBoxItems
都可以绑定到复杂的 Entity
,最终包括从模型数据到的中间类型转换UI 数据并返回,这样我就不必编写大量样板代码来填充视图,然后将 UI 值传递回模型以保存?
2 - Does winforms support databinding in such a way that each of my ListBoxItems
can be bound to a complex Entity
, eventually including an intermediate type conversion from Model data to UI data and back, in such a way that I don't have to write tons of boilerplate code to populate the view and then pass the UI values back into the Model in order to save?
3 - 如果我想引入 Animations
以使当前的 SelectedItem
以动画方式将自身扩展为某种行详细信息"模式,您可以在其中能看到很多附加信息吗?
3 - What if I wanted to introduce Animations
in such a way that the currently SelectedItem
would animatedly expand itself into some kind of "Row Details" mode, where you can see a lot of additional information?
4 - winforms 是否支持 UI 虚拟化
,如果我有,比如 100 万个项目,加载 UI 并只渲染屏幕上可见的内容不需要生命周期?
4 - Does winforms support UI Virtualization
in such a way that if I have, say 1 million items it doesn't take a lifetime to load the UI, and only render what's visible on screen?
5 - 假设我想在方程式中引入复杂的图形.winforms渲染硬件加速吗?
5 - Say I wanted to introduce complex graphics to the equation. Is winforms rendering hardware-accelerated?
6 - 如何使所有这些 Resolution Independent
列表框及其所有内容伸展到可用的窗口大小,以便在保持与较小屏幕的兼容性的同时利用更大的屏幕?
6 - How do I make all this Resolution Independent
in such a way that the ListBox and all its contents stretch to the available window size in order to leverage larger screens while maintaining compatibility with smaller ones?
7 - 建议使用 ListView
控件而不是常规 ListBox
,ListView
是否提供添加任何 UI 的能力进去?例如,我可以为每个项目添加视频吗?还是带有保存和编辑按钮的复杂主/详细模板?
7 - It's been suggested to use the ListView
control instead of a regular ListBox
, does the ListView
provide the ability to add ANY UI into it? can I add Videos for example for each item? or a complex Master/Detail template with Save and edit Buttons?
8 - winforms 是否提供一致且充分的文档模型能够创建高保真所见即所得文档和其他类型的丰富内容?
8 - Does winforms provide a consistent and adequate Document Model that enables the creation of high-fidelity WYSIWYG documents and other types of rich content?
要回答最重要的问题 - 如何在 WinForms 中执行此操作 - 我建议如下:
To answer the overarching question - how to do this in WinForms - I'd suggest the following:
在您的 WinForms 应用程序中使用 WPF ListBox,包装在 ElementHost 中.这有其自身的问题,但我认为这是获得预期效果的最干净的方法.
Use a WPF ListBox in your WinForms application, wrapped in an ElementHost. This has its own issues, but I think it's the cleanest way to get the desired effect.
如果这不符合要求,那么
使用具有支持此功能的组件的第三方控制套件(Infragistics 和 DevExpress 都支持).
Use a third party control suite that has components which support this (Infragistics and DevExpress both do).
旋转您自己的派生 ListBox 控件,该控件覆盖绘制等以呈现所需的内容.
Spin your own derived ListBox control that overrides paint, etc to render the desired content.
针对您的个人问题:
有什么方法可以在 Windows 窗体中实现相同的功能,同时保持 View
和 Application Logic
以这样一种方式,如果我以后想完全重新定义 View
,我就不必重构整个应用程序了?
过去,我在 Windows 窗体中使用过 MVP(模型-视图-演示者)范例.它用于将视图与业务逻辑分离,尽管不如使用 WPF 的 MVVM 方法那么干净.我能给出的最好建议是:不要将业务逻辑放在事件处理程序中.
Is there any way to achieve the same in Windows Forms, all while maintaining separation of concerns
between the View
and the Application Logic
in such a way that if I later wanted to completely redefine the View
, I wouldn't have to refactor the entire application?
In the past, I've used the MVP (model-view-presenter) paradigm with Windows Forms. It works for separating the view from the business logic, albeit not as cleanly as an MVVM approach with WPF. The best advice I can give is: don't put business logic in event handlers.
winforms 是否支持数据绑定,使得我的每个 ListBoxItems
都可以绑定到一个复杂的 Entity
,最终包括一个从模型数据到 UI 数据并返回的中间类型转换,这样我就不必编写大量样板代码来填充视图,然后将 UI 值传递回模型以保存?
不可以.Windows 窗体数据绑定不支持复杂的数据绑定.您可以通过 ICustomTypeDescriptor 或 IBindingSource 自己实现一些东西,它们可以采用复杂的路径并评估它们以用于绑定目的......但没有任何现成可用的东西.
Does winforms support databinding in such a way that each of my ListBoxItems
can be bound to a complex Entity
, eventually including an intermediate type conversion from Model data to UI data and back, in such a way that I don't have to write tons of boilerplate code to populate the view and then pass the UI values back into the Model in order to save?
No. Windows Forms databinding does not support complex data binding. You could implement something yourself via ICustomTypeDescriptor or IBindingSource that can take complex paths and evaluate them for binding purposes...but nothing exists out of the box for this.
如果我想引入 Animations
以使当前的 SelectedItem
动画地扩展为某种"Row Details"模式,在这里可以看到很多额外的信息?
您必须滚动自己的 Windows 窗体 ListBox 和 ListBoxItems 并覆盖绘制操作.
What if I wanted to introduce Animations
in such a way that the currently SelectedItem
would animatedly expand itself into some kind of "Row Details" mode, where you can see a lot of additional information?
You'd have to roll your own Windows Forms ListBox and ListBoxItems and override the paint operations.
winforms 是否支持 UI 虚拟化
,如果我有,比如 100 万个项目,加载 UI 不会花费生命周期,并且只渲染屏幕上可见的内容?
不是开箱即用,但一些第三方控制套件具有支持虚拟化类型的组件......但与 WPF 完全不同.
Does winforms support UI Virtualization
in such a way that if I have, say 1 million items it doesn't take a lifetime to load the UI, and only render what's visible on screen?
Not out of the box, but some third party control suites have components that support types of virtualization...but not at all in the same way WPF does.
假设我想在方程式中引入复杂的图形.winforms 渲染是硬件加速的吗?
Windows 窗体基于 GDI+.GDI+ 没有硬件加速:Windows 7 在 Windows 7 下很慢?
如何使所有这些 Resolution Independent
列表框及其所有内容拉伸到可用的窗口大小,以利用更大的屏幕同时保持与较小的兼容性?
您可以使用 Windows 窗体中的停靠和锚定来完成此操作.或者您可以添加自定义事件处理程序以根据分辨率和窗口大小执行适当的布局调整.
How do I make all this Resolution Independent
in such a way that the ListBox and all its contents stretch to the available window size in order to leverage larger screens while maintaining compatibility with smaller ones?
You can use Docking and Anchoring in Windows Forms to accomplish this. Or you can add custom event handlers to perform appropriate layout adjustments based on resolution and Window size.
建议使用 ListView
控件而不是常规的 ListBox
,ListView
提供向其中添加任何 UI 的能力?例如,我可以为每个项目添加视频吗?还是带有保存和编辑按钮的复杂主/详细信息模板?
这很简单……但 ListView 只是一个支持多种视图类型的 ListBox.它在数据绑定方面也受到更多限制.http://blog.gfader.com/2008/09/winforms-listbox-vs-listview.html.
It's been suggested to use the ListView
control instead of a regular ListBox
, does the ListView
provide the ability to add ANY UI into it? can I add Videos for example for each item? or a complex Master/Detail template with Save and edit Buttons?
This is simplifying...but a ListView is simply a ListBox that supports multiple view types. It is also more limited in terms of databinding. http://blog.gfader.com/2008/09/winforms-listbox-vs-listview.html.
winforms 是否提供一致且充分的 [文档模型][2] 以支持创建高保真所见即所得文档和其他类型的丰富内容?
一点都不.一点也没有.
Does winforms provide a consistent and adequate [Document Model][2] that enables the creation of high-fidelity WYSIWYG documents and other types of rich content?
No. Not at all. Not even a little bit.
简而言之,如果这是一个可以接受的解决方案,我会将您的 WPF ListView 包装在 ElementHost 中并收工.
In short, if it's an acceptable solution, I'd wrap your WPF ListView in an ElementHost and call it a day.
这篇关于ListBoxItem 内的复杂 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!