链接来自不同用户控件的网格列长度
我有一个用户控件,将其命名为 MyUserControl。 MyUserControl 由一个单行多列的网格组成。每列中有一个文本框,每列的宽度设置为 *。
现在,我将 MyUserControl 的三个实例添加到另一个控件 (_myUserControl1,2,3) 的网格中。我想要做的是将 MyUserControls 中某些列的列宽相互链接。
当我在 MyUserControls 之一中的文本框中键入内容时,如果输入的文本比当前文本框宽度长,则文本框宽度会自动增长。我希望其他 MyUserControls 中的相应列也能展开。
例如,我在 _myUserControl1 中的文本框中键入内容,并且 _myUserControl2 和 _myUserControl3 中的列也会展开。
我尝试更新其他控件的 ColumnDefinition 宽度,效果很好。问题是,一旦我设置了 ColumnDefinition 的宽度,它就会失去其星形调整功能。因此,如果我稍后调整窗口大小,这些列将不再填充可用空间。
简而言之,我希望能够将网格列宽度设置为特定大小,并让它保留其星形大小调整功能,这样,如果我正在打字并且文本框变宽,其他控制列也会变宽以匹配。如果我随后调整窗口的宽度,列会展开/收缩,就像为这些列启用了星形大小调整一样,但保持正确的文本框宽度(我想我也必须弄乱列的 MinWidths)。
这是可行的还是我找错了树?我希望单独的 MyUserControls 中的列始终具有相同的大小。
如果问题不清楚,请告诉我,我会尽力使其更清楚。
谢谢。
I have a user control, call it MyUserControl. MyUserControl is made up of a single grid with one row and multiple columns. There is a single textBox in each column and each column's width is set to *.
Now I added three instances of MyUserControl to a grid in another control (_myUserControl1,2,3). What I want to do is link the column widths of some of the columns in the MyUserControls to each other.
When I type in a textBox in one of the MyUserControls, the textBox width grows automatically if the text entered is longer the current textBox width. I want the corresponding columns in the other MyUserControls to expand as well.
For example, I type in a textBox in _myUserControl1 and the columns in _myUserControl2 and _myUserControl3 expand as well.
I tried updating the ColumnDefinition widths of the other controls and that works fine. The problem is that once I set the width of a ColumnDefinition, it loses its star sizing capability. As a result, if I later resize the window, the columns no longer fill the available space.
In a nutshell, I want to be able to set a grid column width to a specific size and have it retain its star sizing capability so that if I am typing and a textBox grows wider, the other control columns grow wider to match. If I then resize the width of the window, the columns expand/contract as if star sizing was enabled for those columns but maintaining the correct textBox width (I guess I would have to mess around with the MinWidths of the columns as well).
Is this doable or am I barking up the wrong tree? I want the columns in the separate MyUserControls to always be the same size.
Please let me know if the question is not clear and I will try my best to make it clearer.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是保持 MyUserControl 的每个实例的列宽相等的一种方法。如果我想出更干净的解决方案,我会更新。
首先,我们在 MyUserControls 代码隐藏文件中创建一个静态二维文本框列表。第一个列表是第 0 列中 TextBox 的所有实例的列表。第二个列表用于第 1 列,第三个列表用于第 2 列。
在静态构造函数中,我们对其进行初始化。
然后,在构造函数中,我们为此实例添加文本框
Add每个 TextBox 的 TextChanged 事件
在 EventHandler 中,我们检查该事件适用于哪个 TextBox
最后,我们测量该列的所有 TextBox 并将 MinWidth 设置为最宽 TextBox 的宽度
This is one way of keeping the column width equal for every instance of MyUserControl. I'll update if I come up with a cleaner solution.
First we create a static 2d List of TextBoxes in MyUserControls code behind file. The first List is a List of all instances of TextBox in column 0. The second List is for column 1 and the third List if for column 2.
In the static constructor we initialize it
In the constructor we then add the TextBoxes for this instance
Add a TextChanged event for every TextBox
In the EventHandler we check which TextBox this event is for
And finally we Measure all the TextBoxes for this column and set the MinWidth to the Width of the widest TextBox