WPF [SOLVED]: WPF grid sizing Auto vs Star

WPF [SOLVED]: WPF grid sizing Auto vs Star

Home Forums Frameworks WPF WPF [SOLVED]: WPF grid sizing Auto vs Star

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246867

    Cloudy Point
    Keymaster

    QuestionQuestion

    In my XAML I have a certain amount of horizontal space. In it I need to put a text (of unknown length) followed by a button. Button should be placed directly after text. I am using following XAML:

    <Border Name="Boundaries" MaxWidth="500" MinWidth="500" Height="20">
          <Grid>
             <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" TextTrimming="CharacterEllipsis">
                    Lorem ipsum dolor sit amet, pri ex option legendos. Ludus solet at mel, facilisis urbanitas nam ad.
                </TextBlock>
             <Button Grid.Column="1">Button</Button>
         </Grid>
    </Border>
    

    Everything works fine when the text in TextBlock is short, however when text is long enough, TextBlock will overflow parent element and button will not be visible. To fix this I could set width of the first column definition to *, but this would mean that button is always on the right (even when text is single word, but I want it right after the text).

    In past I fixed similar problems by setting MaxWidth of the first column definition trough converter from ActualSize of parent, however this is quite complicated and highly unreliable.

    It would seem that such basic layout requirement would be a part of WPF, but I could not find anything. Am I missing something?

    I want something like this:
    enter image description here

    #246868

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You could use a DockPanel to achieve this. Something like:

    <Border Name="Boundaries" MaxWidth="500" MinWidth="500" Height="20">
        <DockPanel HorizontalAlignment="Left" LastChildFill="False">
            <Button DockPanel.Dock="Right">Button</Button>
            <TextBlock DockPanel.Dock="Right" TextTrimming="CharacterEllipsis">
                Lorem ipsum dolor sit amet, pri ex option legendos. Ludus solet at mel, facilisis urbanitas nam ad.
            </TextBlock>
        </DockPanel>
    </Border>
    

    Source: https://stackoverflow.com/questions/47948123/wpf-grid-sizing-auto-vs-star
    Author: Chris Mack
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.