链接来自不同用户控件的网格列长度

发布于 2024-10-02 19:34:09 字数 750 浏览 0 评论 0原文

我有一个用户控件,将其命名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-10-09 19:34:09

这是保持 MyUserControl 的每个实例的列宽相等的一种方法。如果我想出更干净的解决方案,我会更新。

alt text

首先,我们在 MyUserControls 代码隐藏文件中创建一个静态二维文本框列表。第一个列表是第 0 列中 TextBox 的所有实例的列表。第二个列表用于第 1 列,第三个列表用于第 2 列。

private static List<List<TextBox>> s_textBoxesColumns;

在静态构造函数中,我们对其进行初始化。

static MyUserControl()
{
    s_textBoxesColumns = new List<List<TextBox>>();
    s_textBoxesColumns.Add(new List<TextBox>());
    s_textBoxesColumns.Add(new List<TextBox>());
    s_textBoxesColumns.Add(new List<TextBox>());
}

然后,在构造函数中,我们为此实例添加文本框

public MyUserControl()
{
    InitializeComponent();
    s_textBoxesColumns[0].Add(textBox1);
    s_textBoxesColumns[1].Add(textBox2);
    s_textBoxesColumns[2].Add(textBox3);
}

Add每个 TextBox 的 TextChanged 事件

<TextBox Name="textBox1" Grid.Column="0" TextChanged="textBox_TextChanged"/>
<TextBox Name="textBox2" Grid.Column="1" TextChanged="textBox_TextChanged"/>
<TextBox Name="textBox3" Grid.Column="2" TextChanged="textBox_TextChanged"/>

在 EventHandler 中,我们检查该事件适用于哪个 TextBox

private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (sender == textBox1)
    {
        SetWidthOfTextBoxes(0);
    }
    else if (sender == textBox2)
    {
        SetWidthOfTextBoxes(1);
    }
    else if (sender == textBox3)
    {
        SetWidthOfTextBoxes(2);
    }
}

最后,我们测量该列的所有 TextBox 并将 MinWidth 设置为最宽 TextBox 的宽度

private void SetWidthOfTextBoxes(int column)
{
    double width = 0.0;
    foreach (TextBox textBox in s_textBoxesColumns[column])
    {
        textBox.MinWidth = 0.0;
        textBox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        if (textBox.DesiredSize.Width > width)
        {
            width = textBox.DesiredSize.Width;
        }
    }
    foreach (TextBox textBox in s_textBoxesColumns[column])
    {
        textBox.MinWidth = width;
    }
}

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.

alt text

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.

private static List<List<TextBox>> s_textBoxesColumns;

In the static constructor we initialize it

static MyUserControl()
{
    s_textBoxesColumns = new List<List<TextBox>>();
    s_textBoxesColumns.Add(new List<TextBox>());
    s_textBoxesColumns.Add(new List<TextBox>());
    s_textBoxesColumns.Add(new List<TextBox>());
}

In the constructor we then add the TextBoxes for this instance

public MyUserControl()
{
    InitializeComponent();
    s_textBoxesColumns[0].Add(textBox1);
    s_textBoxesColumns[1].Add(textBox2);
    s_textBoxesColumns[2].Add(textBox3);
}

Add a TextChanged event for every TextBox

<TextBox Name="textBox1" Grid.Column="0" TextChanged="textBox_TextChanged"/>
<TextBox Name="textBox2" Grid.Column="1" TextChanged="textBox_TextChanged"/>
<TextBox Name="textBox3" Grid.Column="2" TextChanged="textBox_TextChanged"/>

In the EventHandler we check which TextBox this event is for

private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (sender == textBox1)
    {
        SetWidthOfTextBoxes(0);
    }
    else if (sender == textBox2)
    {
        SetWidthOfTextBoxes(1);
    }
    else if (sender == textBox3)
    {
        SetWidthOfTextBoxes(2);
    }
}

And finally we Measure all the TextBoxes for this column and set the MinWidth to the Width of the widest TextBox

private void SetWidthOfTextBoxes(int column)
{
    double width = 0.0;
    foreach (TextBox textBox in s_textBoxesColumns[column])
    {
        textBox.MinWidth = 0.0;
        textBox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        if (textBox.DesiredSize.Width > width)
        {
            width = textBox.DesiredSize.Width;
        }
    }
    foreach (TextBox textBox in s_textBoxesColumns[column])
    {
        textBox.MinWidth = width;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文