WPF自定义控件-具体如何获取正确的宽度比例
我正在创建一个控件来代表员工轮班。轮班可以有不同的长度,并且没有或多次休息。
9 小时轮班、1 小时休息的 GUI 样机
以下问题涉及原型中的蓝色条: 由于控件需要完美地调整大小,因此固定大小的方法不是一个选择。我的第一个想法是使用具有与时间跨度相同的宽度比的列的网格。因此,如果您查看上面的原型,就会发现 3 列的宽度为:240*、60*、240*。这些数字等于每个时间跨度的总分钟数。
如果我添加一个保持的依赖属性,我们将它们称为 TimeSpanItems (TSI)。每个 TSI 都有一个 TimeSpan 属性。那么是否可以将其绑定到网格及其列定义?随着 TSI 的添加,列数必须改变,并且每列必须改变其宽度比以匹配分钟数。
我是否以错误的方式思考这个问题?可行吗?或者它是一个项目控件,我需要在调整控件大小时调整其项目的大小?
目前我有不同的问题,但我还没有找到答案......并且可能有很多问题我还不知道它们是什么。任何帮助将是非常受欢迎的。
I'm in the process of creating a control to represent a employee work shift. The shift can be of different lengths and with none or more breaks.
Mockup prototype GUI of a 9 hour work shift with an 1 hour break
The following questions refer to the blue bar in the prototype:
Since the control need to resize perfectly, then a fixed size approach is not an option. My first thought was to use a grid with columns that has the same width ratio as the time spans. So if you look at the prototype above there would be 3 columns with a width of: 240*, 60*, 240*. These numbers are equal to the total minutes of each time span.
If I add a dependency property that hold, lets call them TimeSpanItems (TSI). Each TSI has a TimeSpan property. Is it then possible to bind this to the grid and its column definitions? The number of columns must change as TSI are added and also each column must change its width ratio to match the number of minutes.
Am I thinking about this the wrong way? Is it doable? Or is it a items control that I need that resizes its items when the control is resized?
At the moment I have different questions that I yet haven't found the answer to... and probably a lot of questions that I don't know yet what they are. Any help would be most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了一些可能对你有帮助的事情,最终的结果是这个控件:
基本上列出了任务,在这些里面它显示顶部列出的“实体”的具体分配任务。
任务列表是一个 ItemsControl,如下所示:
列宽由该转换器给出。在构建此控件时,我们没有能力扩展模型,因此编写了一个转换器,如果现在完成此操作,则将扩展模型并编写 Getter。
转换器看起来像这样:
I did something that could be of help to you, the final result is this control:
Basically lists tasks, inside these tasks it's showing specific alocation by the "entities" that are listed on top.
The Task list is an ItemsControl that looks like this:
The column width's are given by that converter. At the time this control was built we didn't had the ability to extend the Model, so a converter was written, if this was done now, the Model would have been extended and a Getter would have been writen.
The Converters look like this:
我来回答这个问题:可行吗?我最初的想法是我不想计算调整大小。我想要一个网格来帮我解决这个问题。因此,我将忽略所有其他问题,即这是否应该是另一种控制类型,或者这是否是“正确”的方法。
首先,我使用一个控件模板,其中 uxMainContentGrid 是添加项目时将要修改的网格。
我用来设置宽度比例的项目如下所示。
RatioItems 通过此依赖项属性添加,默认值为空的 RatioItems 列表。
具有以下值的依赖属性更改了回调方法。当您绑定到依赖项属性时,此回调会在应用模板之前触发。如果已设置控件模板,则此回调仅重建网格列。
以下代码在应用控件模板时检索网格。这里的目的是存储网格,以便以后在 RatioItems 属性发生更改时可以访问它。应用模板时,绑定已经生效,因此网格列已构建。
这个方法完成了所有的“构建”......
现在你已经完成了。这是可以做到的。现在讨论这是否是“正确”的方法......:)
I'm going to answer the question: Is it doable? My initial thought was that I didn't want the calculate the resizing. I wanted a grid to solve this for me. So I am going to ignore all the other questions regarding if this should be another control type or if this the "right" way to do it.
Here goes... first I use a control template where the uxMainContentGrid is the grid that is going to be modified when items are added.
The items that I used to set the width ratio look like this.
And the RatioItems are added through this dependency property with a default value of an empty list of RatioItems.
The dependency property that has the following value changed callback method. When you bind to the dependency property, then this callback fires before the template has been applied. This callback only reconstructs the grid columns if the control template has been set.
The following code retrieves the grid when the control template is applied. The thing here is to store the grid so that it can be accessed later if the RatioItems property change. When the template is applied the binding is already in affect so the grid columns are constructed.
This method does all the "constructing"...
And there you have it. It can be done. Now to the discussion whether it is the "right" way to do it... :)