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

  • Home
  • SEARCH
  • 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 7493651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:06:15+00:00 2026-05-29T17:06:15+00:00

I am placing a templated Button with some content in inside it (both Image

  • 0

I am placing a templated Button with some content in inside it (both Image and TextBlocks) in some other control’s Grid:

<SomeControl>
   <Grid>
       <SpecialButton/>
   </Grid>
</SomeControl>

<Style TargetType="{x:Type local:SpecialButton}">>
    ...
    <Setter Property="Template">
        <Setter.Value>
        ...
            <TextBlock/>
            <TextBlock/>
        </Setter.Value>
</Style>

The “SomeControl” control’s width is dynamic – that is – it can be dynamically changed according to the screen width, it’s container width, etc. Hence I use another method to calculate the “SpecialButton”‘s width.

As long as the TextBlock within the button is not wider then the button itself The “SpecialButton” is being perfectly matching its size to it’s “SomeControl” container.
However, if the TextBlock’s text is longer then the width of the “SpecialButton”, the “SpecialButton” right border disappears and the button looks really bad. That being said, the “SpecialButton”‘s width is still constained to its containers’ width although drawing is not being well calculated.
I am trying to find a way to constain the TextBlock’s (or even better the complete Grid’s width) without defining an absoulte width, so the “SpecialButton” will still get drawn well and ignore it’s children’s overflow. Something like CSS’s {overflow:hidden} would have been great…

Note:

I can’t allow wrapping since the “SpecialButton” height is also constained (and manually calculated).

  • 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-05-29T17:06:20+00:00Added an answer on May 29, 2026 at 5:06 pm

    Width (and height) in WPF elements is pretty straightforward once you get used to it. Your grid, by default is set to Auto width, which means it will request 100% of the width of the parent.

    Instead of setting a maximum size, I generally put items that I want dynamically sized and positioned in a grid and set grid row and column sizes. So if I want something to take up 1/3 of the available horizontal space, I can have a grid with two columns, one with width set to “1*” and the other set to “2*” (2/3 being twice the size of 1/3), and put my item in column 1 set to take up all available width.

    In your case, the text needs to be truncated as it is doing its best to render all of the text, and is extending the button past the bounds of it’s container to do it. There are two options for the built in trimming capability. TextTrimming="CharacterEllipsis" and TextTrimming="WordEllipsis" Just put that in your textblocks. I would put the textblocks in a container of some kind to keep them separate. If the first has content that is long enough, it may push the other completely off the side.

    Element Sizing

    <Window
        x:Class="MainWindow"
        x:Name="Window"
        Title="MainWindow"
        Width="640" Height="480">
        <Grid x:Name="LayoutRoot" DataContext="{Binding}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <local:SomeControl Margin="8,8,8,0"/>
        </Grid>
    </Window>
    

    Text Trimming

    <ControlTemplate x:Key="ButtonBaseControlTemplate1" TargetType="{x:Type ButtonBase}">
        <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}" SnapsToDevicePixels="True" ThemeColor="Metallic">
            <TextBlock Text="{TemplateBinding Content}" TextTrimming="WordEllipsis" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Microsoft_Windows_Themes:ButtonChrome>
        <ControlTemplate.Triggers>
            <Trigger Property="IsKeyboardFocused" Value="True">
                <Setter Property="RenderDefaulted" TargetName="Chrome" Value="True"/>
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter Property="RenderPressed" TargetName="Chrome" Value="True"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    Additionally, you might want to set the tooltip of your textboxes to the value of the text also. If the text is truncated and a user want to see what it is, they can mouse over the button and find out.

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

Sidebar

Related Questions

I am placing a single button with an image in the header of a
I'm placing content on my page through an ajax (post) request like so: $("input#ViewMore").click(function()
I was looking at this question and noticed that placing an implicit TextBlock style
I have a template for my list box item. It includes an image button.
I have built a custom control that extends content control. Within this I have
When placing email addresses on a webpage do you place them as text like
A C-program is placing what it considers to be 64-bit unsigned integers into a
i'm placing a automatic redirect on my Classic ASP page (vb). i want to
I am placing a JS file to remote server(s). I d like to know
Are there benefits to placing ASP.Net code in a separate file?

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.