开发者

Linking grid column lengths from different user controls

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.


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.

Linking grid column lengths from different user controls

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜