在 WPF 中创建可重用的界面元素
我正在用 C# 开发一个 WPF 应用程序,并正在考虑跨各种窗口实现自定义 UI 元素。
我想要一个最小化的托盘(仅可见约 4px),单击托盘旁边的图标后可以展开。扩展版本将显示所有控件,并在我再次单击该图标时最小化。我创建了一个快速 HTML 概念 来澄清事情。
我知道我可以在我的应用程序中放置一个堆栈面板和按钮,并在单击按钮时使它们都向上移动,但随后我需要大量复制代码。
虽然我对 C# 很有经验,但我对 WPF 界面开发/模板相当陌生,但我确信必须有一种方法,以便我可以在我的应用程序中使用该 UI 元素,而无需复制/粘贴大量内容我的表单类文件中的代码行。
我希望有人可以帮助我,或者至少为我指出正确的方向。
I'm developing a WPF application in C# and was thinking about implementing a custom UI element accross various windows.
I would like to have a minimized tray (only about 4px visible) that expands after clicking on an icon next to the tray. The expanded version would show all controls and would minimize when I click the icon again. I created a quick HTML concept to clarify things.
I know I could put a stackpanel and button in my application and making both of them move up when I click the button, but then I would need to duplicate the code a lot.
Though I'm experienced with C#, I'm fairly new to WPF interface development/templates, but I'm sure there has to be a way so I can use that UI element accross my application without needing to copy/paste a lot of lines of code in my form class file.
I hope someone can help me, or at least point me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可通过三种方式自定义元素。
1 如果您只需要视觉修改,您可以使用样式来更改 .net 默认控件的外观。您甚至可以覆盖/扩展默认模板。
2 如果您希望在控件中自定义逻辑,您可以创建自定义控件。该框架带来了许多可供构建的“原语”。例如
ContentControl
或HeaderedContentControl
。假设您想要构建自定义扩展器控件,您可以从HeaderedContentControl
继承自定义控件,它为您提供 Header 和 Content 属性,您只需自己实现切换逻辑即可。如果您想构建可在整个应用程序中使用的基本功能,CustomControls 是一个不错的选择。它们也可以根据用例进行主题/样式设置(参见 1)。
3 如果您想将不同的控件组合成一个控件,您可以创建一个UserControl。用户控件是使用 XAML 组成的。大多数顶层控件是由视图模型驱动的用户控件。
您的案例可以使用
Popup
和ToggleButton
或Expander
构建。该决定取决于所需的行为。如果您希望打开的面板将以下内容向下移动,您需要一个扩展器。如果您想要类似下拉菜单的功能,则需要弹出窗口。
如果您使用弹出窗口,只需将
IsPopupOpen
属性绑定到 ToggleButton 的IsChecked
并设置PopupStaysOpen = false
将按钮连接到弹出窗口。如果您使用扩展器控件,则应创建一个可应用于应用程序中所有相同扩展器的样式,以最大限度地减少每个视图中所需的 XAML。
There are three ways to customize your elements.
1 If you only need visual modifications you can use styles to change the appearance of the .net default controls. You can even override / extend the default templates.
2 If you want custom logic in a control you can create a custom control. The framework brings a lot of "primitives" to build upon. Examples are
ContentControl
orHeaderedContentControl
. Say you want to build a custom expander control you can inherit your custom control fromHeaderedContentControl
which provides you with Header and Content properties and you just have to implement the toggling logic yourself.CustomControls are a good choice if you want to build basic functionality which can be used throughout your application. They can be themed/styled depending on the use case, too (see 1).
3 If you want to compose different controls into one control you can create a UserControl. User controls are composed using XAML. Most top level controls are user controls driven by a view model.
Your case can be build using a
Popup
andToggleButton
or anExpander
.The decision depends on the desired behavior. If you want the opened panel to move following content down you need a expander. If you want a dropdown like functionality you need popup.
If you use a popup just bind the
IsPopupOpen
Property toIsChecked
of the ToggleButton and setPopupStaysOpen = false
to wire the button to your popup.If you use an expander control you should create a style which can be applied to all equal expanders in your application to minimize the required XAML in each view.
使用扩展器控制怎么样?
How about using Expander Control?
有一个称为 Expander 的控件非常适合此操作。您必须将其设计为您想要的样式,但它具有您想要的内置功能。
There's a control called an Expander that is perfect for this. You'll have to style it to look like you want, however it has the functionality you want built-in.