Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8448677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:29:36+00:00 2026-06-10T10:29:36+00:00

What I’m looking to do is manage a textblock and combobox in a layout

  • 0

What I’m looking to do is manage a textblock and combobox in a layout grid. The combobox needs to be sized to fit it’s selection (Width=Auto). The textblock needs to be trimmed with an ellipse if necessary, OR when it is not trimmed the column should stop expanding past the textblocks width – instead the white space should grow to the right of the combo. So, I’m kind of looking for a mix of Width=”*” while the textblock is being trimmed and Width=”Auto” when it is not being trimmed. It would be great if MaxWidth supported “Auto”.

I don’t want to set a specific MaxWidth of the textblock column because that will change with globalized values. I also would prefer it if there was a xaml only solution, but if there isn’t, Oh Well. Also, maybe the grid is not the correct container for this.

Here is an example. The left column has Width=”*” and MaxWidth=”150″. This works correctly, but specifying a MaxWidth like this will not work with globalized text in the textblocks. I need something more dynamic.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" MaxWidth="150"/>
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock HorizontalAlignment="Left" Text="Input control one" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" Margin="2"/>
    <TextBlock HorizontalAlignment="Left" Text="Input control two" VerticalAlignment="Center" Grid.Row="1" TextTrimming="CharacterEllipsis" Margin="2"/>
    <TextBlock HorizontalAlignment="Left" Text="Input control number three" VerticalAlignment="Center" Grid.Row="2" TextTrimming="CharacterEllipsis" Margin="2"/>
    <ComboBox HorizontalAlignment="Left" d:LayoutOverrides="Height" VerticalAlignment="Center" Grid.Column="1" Margin="2">
        <ComboBoxItem Content="Item One" IsSelected="True"/>
    </ComboBox>
    <ComboBox HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Margin="2">
        <ComboBoxItem Content="Item One" />
        <ComboBoxItem Content="Item Number Two" IsSelected="True"/>
    </ComboBox>
</Grid>

Solution…

Thanks Leo for getting me to the solution. I just needed to figure out how calculate the MaxWidth from the attached property. The key was to get the maximum desired width of all the elements in the column. It was also necessary to re-measure the element using infinity to get the full desired width. Otherwise, you could get a trimmed desired width.

    /// <summary>
    /// AutoMaxWidth Dependency Property allows a ColumnDefinition to automatically default
    /// it's MaxWidth to the correct width on the Load event of the ColumnDefinition. 
    /// </summary>
    public static readonly DependencyProperty AutoMaxWidthAttachedProperty =
            DependencyProperty.RegisterAttached ("AutoMaxWidth", 
                                                 typeof (bool), 
                                                 typeof (ColumnBehavior),
                                                 new UIPropertyMetadata (false, OnAutoMaxWidthAttachedPropertyChanged));

    /// <summary>
    /// Called when the AutoMaxWidth property has changed values
    /// </summary>
    /// <param name="o">The object this behavior is attached to.</param>
    /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
    static void OnAutoMaxWidthAttachedPropertyChanged (DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        ColumnDefinition col = (o as ColumnDefinition);
        if (col == null) return;

        if ((bool)e.NewValue)
            col.Loaded += col_Loaded;
        else
            col.Loaded -= col_Loaded;
    }

    static void col_Loaded (object sender, RoutedEventArgs e)
    {
        ColumnDefinition col = sender as ColumnDefinition;
        if (col == null) return;

        Grid g = col.Parent as Grid;
        if (g == null) return;

        int index = g.ColumnDefinitions.IndexOf (col);
        foreach (UIElement el in g.Children)
            if (Grid.GetColumn (el) == index)
            {
                el.Measure (new Size (Double.PositiveInfinity, el.DesiredSize.Height));
                if (col.MaxWidth == Double.PositiveInfinity)
                    col.MaxWidth = el.DesiredSize.Width;
                else
                    col.MaxWidth = Math.Max (col.MaxWidth, el.DesiredSize.Width);
            }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:29:37+00:00Added an answer on June 10, 2026 at 10:29 am

    You either want to explicitly set the MaxWidth of the ColumnDefinition or you want to create your own logic to set the MaxWidth when the TextBox/ComboBox width updates. You could achieve this using attached properties and attach it to the Grid.ColumnDefinitions. WPF is powerful that you can do whatever you want as long as you know how to use it.

    Could you provide an example code of what you have right now if you don’t mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I would like to count the length of a string with PHP. The string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.