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 478699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:43:05+00:00 2026-05-13T00:43:05+00:00

I have a TextBlock that’s on top of a Button in a Grid .

  • 0

I have a TextBlock that’s on top of a Button in a Grid. I’d like to have then displayed thus:


"some very long text" <-- text
"that doesn't fit" <-- text wrapped
[my button text size] <-- button

However, what I’ve got is this:


"some very long text that doesn't fit" <-- text
[my button text size] <-- button

My issue is that the text in the Button is dynamically set through localized resource and therefore the width of the button changes dynamically.

The static solution that works for non-dynamic Button resize is:

                <TextBlock
                    Margin="5"
                    TextWrapping="Wrap"
                    Width="{Binding ElementName=requestDemoButton, Path=RenderSize.Width}"
                    Text="{Binding Path=Resource.Text, Source={StaticResource LocalizedStrings }}"
                    />
                <Button 
                    x:Name="requestDemoButton"
                    Margin="5" 
                    Height="Auto" 
                    Width="Auto" 
                    HorizontalAlignment="Right"
                    Content="{Binding Path=Resource.Button, Source={StaticResource LocalizedStrings }}" />

Ideas, anyone? I’m currently thinking of sticking a Behavior class to the TextBlock that listens for the SizeChanged event on the Button. I’d like to have a built-in solution if it exists.

  • 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-13T00:43:05+00:00Added an answer on May 13, 2026 at 12:43 am

    If anyone’s interested, here’s how I’ve done in in a behaviour.
    I pass on Height or Width according to the property I want bound.

    Here’s the class:

    public static class DynamicControlResizeBehavior
    {
        public static readonly DependencyProperty TargetProperty =
            DependencyProperty.RegisterAttached("Target", typeof(FrameworkElement), typeof(DynamicControlResizeBehavior), new PropertyMetadata(OnTargetSetCallback));
    
        public static readonly DependencyProperty PropertyNameProperty =
            DependencyProperty.RegisterAttached("PropertyName", typeof(string), typeof(DynamicControlResizeBehavior), new PropertyMetadata(OnPropertyNameSetCallback));
    
    
        public static string GetPropertyName(DependencyObject obj)
        {
            return (string)obj.GetValue(PropertyNameProperty);
        }
    
        public static void SetPropertyName(DependencyObject obj, string value)
        {
            obj.SetValue(PropertyNameProperty, value);
        }
    
        public static FrameworkElement GetTarget(DependencyObject obj)
        {
            return (FrameworkElement)obj.GetValue(TargetProperty);
        }
    
        public static void SetTarget(DependencyObject obj, FrameworkElement value)
        {
            obj.SetValue(TargetProperty, value);
        }
    
        private static void SynchronizeProperty(DependencyObject dependencyObject)
        {
            var target = GetTarget(dependencyObject);
            if (target != null)
            {
                var propertyName = GetPropertyName(dependencyObject);
    
                DependencyProperty dependencyToRead;
                DependencyProperty dependencyToWrite;
                if (string.Equals(propertyName, "Width", StringComparison.InvariantCulture))
                {
                    dependencyToRead = FrameworkElement.ActualWidthProperty;
                    dependencyToWrite = FrameworkElement.WidthProperty;
                }
                else if (string.Equals(propertyName, "Height", StringComparison.InvariantCulture))
                {
                    dependencyToRead = FrameworkElement.ActualHeightProperty;
                    dependencyToWrite = FrameworkElement.HeightProperty;
                }
                else
                {
                    return;
                }
    
                var propertySize = (double)target.GetValue(dependencyToRead);
                dependencyObject.SetValue(dependencyToWrite, propertySize);
            }
        }
    
        private static void OnTargetSetCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var oldElement = e.OldValue as FrameworkElement;
            if (oldElement != null)
            {
                oldElement.SizeChanged -= (o, s) => SynchronizeProperty(d);
            }
    
            var newElement = e.NewValue as FrameworkElement;
            if (newElement != null)
            {
                newElement.SizeChanged += (o, s) => SynchronizeProperty(d);
            }
        }
    
        private static void OnPropertyNameSetCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SynchronizeProperty(d);
        }
    }
    

    And here’s how it’s used:

                <TextBlock
                    Behaviors:DynamicControlResizeBehavior.Target="{Binding ElementName=submitButton}"
                    Behaviors:DynamicControlResizeBehavior.PropertyName="Width"
                    HorizontalAlignment="Right"
                    Margin="5,20,5,5"
                    TextWrapping="Wrap"
                    Text="{Binding Path=Resource.RequestDemoLoginText, Source={StaticResource LocalizedStrings }}"
                    />
                <Button 
                    x:Name="submitButton"
                    Margin="5" 
                    Height="Auto" 
                    Width="Auto" 
                    HorizontalAlignment="Right"
                    HorizontalContentAlignment="Left"
                    Content="{Binding Path=Resource.RequestDemoLogin, Source={StaticResource LocalizedStrings }}" />
    

    Hope that might help someone else.

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

Sidebar

Related Questions

I have a textblock with a long text that is displayed in several lines
I have a TextBlock in my XAML that has its text bound to a
I have TextBlock that has Inlines dynamicly added to it (basically bunch of Run
I have a grid in xaml that has two columns with widths 4.5* and
I have a simple TextBlock like this (it has to be a TextBlock): <ScrollViewer
Here's my problem: I have a datagrid that is bound to a collection of
Hi i have a xaml code like this <Window xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml x:Class=test.Window1 x:Name=Window Title=Window1
I have a text box which is bound to a data service to get
I'm presenting text in a wpf TextBlock control (.Net 3.5). The content of the
I have a WPF window with a tab control, and I'm defining the TabItems

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.