如何切换 WPF 网格列可见性
我在让它在我正在开发的 WPF 应用程序中工作时遇到一些困难。 基本上,我想要的类似于 MMC 中的任务窗格:
- 该应用程序在显示屏的主要部分有三列。 我需要右侧有一列可调整大小。 我认为这意味着使用带有 GridSplitter 的 Grid,但任何有效的方法都可以。
- 我希望能够在应用程序关闭时保存右侧列的宽度,并在应用程序打开时加载它,但这应该是初始大小:用户应该能够调整它的大小。
- 当我调整窗口大小时,我希望左侧和右侧列保持相同大小,中间列随窗口宽度调整大小。
- 左侧和右侧的列需要有最小宽度。 当我调整右侧列的大小时,我希望中心列变小,但左侧列不变。
- 我还希望能够使用列外部的切换按钮切换右侧列的可见性,当它返回可见性时,我希望它的宽度与之前相同。
我尝试在 XAML 和绑定中尽可能多地进行操作。
我可以在上面加奶油、冰淇淋和巧克力片吗? :-)
I'm having some trouble getting this to work in a WPF app I'm working on. Basically, what I'm after is something like the Task pane in an MMC:
- The app has three columns in the main part of the display. I need a column on the right side which is resizable. I presume this means using a Grid with a GridSplitter but anything that works will do.
- I want to be able to save the width of the right-side column when the app is closed and load it when the app is opened but this should be an initial size: the user should be able to resize it.
- When I resize the window, I want the left- and right-side columns to stay the same size and the middle column to resize with the window width.
- The left- and right-side columns need to have a minimum width. When I resize the right-side column I want the centre column to get smaller but not the left-side column.
- I also want to be able to toggle the visibility of the right-side column with a toggle button which is outside the column and when it returns to visibility I want it to be the same width it was before.
I'm trying to do as much as possible in XAML and with binding.
And can I have it topped with cream, ice cream and chocolate chips, please? :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我阅读您的需求时,我想到的不是
Grid
,而是DockPanel
。如果您想办法调整
right
的大小,那么middle
将随着right
大小的调整而改变。 如果调整窗口大小,则只有middle
会发生变化。 存储和设置right
的Width
由您决定,但应该不难。至于允许用户向右调整大小,这会有点棘手,但我发现这篇文章应该会有所帮助。 另一篇文章可能会有所帮助。
对于
right
的可见性,可以将其Visibility
设置为Collapsed
来隐藏它,设置为Visible
即可恢复它代码>.注意:里面的面板不必是
Grid
,但您需要为每个面板使用某种Panel
。 当前 Grid 列中的任何内容都应该可以正常工作。As I read your requirements, instead of thinking of a
Grid
, I think of aDockPanel
.If you make a way to resize
right
, thenmiddle
will change asright
is resized. If you resize the window, onlymiddle
will change. Storing and setting theWidth
ofright
is up to you, but shouldn't be hard.As for allowing the user to resize
right
, that will a bit trickier, but I found this article that should help. This other article might help even more.For the visibility of
right
, you can set itsVisibility
toCollapsed
to hide it and restore it by setting it toVisible
.Note: The panels inside don't have to be
Grid
s, but you will want to use some sort ofPanel
for each. Whatever you have inside your currentGrid
columns should work just fine.我使用了带有 GridSplitters 的网格,因为这使得在保持左右列宽度的同时调整中间列的大小变得非常容易。
XAML:
代码隐藏
当隐藏右列时,我只是将列宽设置为 0,因为网格列没有可见性属性。
至于在启动时保存和恢复列宽度,我只需将宽度变量存储到设置文件中,然后在重新打开应用程序时应用它们。
I used a Grid with GridSplitters since this made it really easy to resize the middle column while maintaining the widths of the left and right columns.
XAML:
Code-Behind
When hiding the right column, I just set the column width to 0 since grid columns don't have a visibility property.
As for saving and restoring the column widths on startup, I would just store the width variables to a settings file and then apply them when your app is reopened.
将列定义宽度设置为自动,并在该列中放置一个控件,并为其他列提供星号。 每当您想要隐藏包含内容的列时,请设置 control.Visibility=Collapsed ,并且由于列宽为“自动”,您将看不到该列,其余列将占用空间。
Set the columndefinition Width to Auto and put a control inside that column and give Star for the other columns . Whenever you want to hide the column with content, set the control.Visibility=Collapsed and since column width is Auto, you wont see that column and the remaining columns will take the space.
3 年后,您可以在 CodeProject 上找到另一种方法。
http://www.codeproject.com/Articles/437237 /WPF-Grid-Column-and-Row-Hiding
它向自定义列定义添加了“可见”属性。
3 years later you can find another approach on CodeProject.
http://www.codeproject.com/Articles/437237/WPF-Grid-Column-and-Row-Hiding
It adds a "Visible" property to custom Column definitions.