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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:29:09+00:00 2026-06-18T04:29:09+00:00

I have a TextBlock control in my view, its Width depends on the Text

  • 0

I have a TextBlock control in my view, its Width depends on the Text property.

I’m looking for some way to bind the TextBlocks Width to a property in my model,which will work as follows:

  1. The setting of the Width must be done automatically based on Text
  2. In my button click I would like to retrieve the Width

I’ve tried the code below, but it keeps the Width as 0 if I don’t explicitly set it in the constructor of the view model.Tried Mode=OneWayToSource and Mode=OneWay but it made no difference, any suggestions?

View:

<Grid>
    <TextBlock Text="Some text" Width="{Binding TextWidth,Mode=OneWayToSource}" />
    <Button Content="Show Width" Height="30" Width="90" Command="{Binding ShowTextWidth}" />
</Grid>

View model:

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

private DelegateCommand<object> showTextWidth;
public DelegateCommand<object> ShowTextWidth
{
    get { return showTextWidth; }
    set { showTextWidth = value; }
}

private double textWidth;
public double TextWidth
{
    get { return textWidth; }
    set 
    { 
        textWidth = value;
        OnPropertyChanged("TextWidth");
    }
}

public ViewModel()
{
    //If I explicitly specify the width it works:
    //TextWidth = 100;
    ShowTextWidth = new DelegateCommand<object>(ShowWidth);
}

private void ShowWidth(object parameter)
{
    MessageBox.Show(TextWidth.ToString());
}

}

  • 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-18T04:29:10+00:00Added an answer on June 18, 2026 at 4:29 am

    Ended up creating an attached behavior by Maleak which was inspired by Kent Boogaarts Pushing read-only GUI properties back into ViewModel, can’t believe it’s so complicated to push the value of ActualWidth into the view model!

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

Sidebar

Related Questions

I have created a listbox control with following DataTemplate <DataTemplate x:Key=lb_Itemtemplate> <DockPanel> <TextBlock Text={Binding}
I have a view model with some fields, i.e. type ViewModel = member x.a
We have a screen and its view model: public class ScreenViewModel : BaseViewModel {
I have some text that is displayed at run time in a textblock. I
I have a View Model object that has a depednency property called IsSearching ;
I have a WPF view that displays a Shipment entity. I have a textblock
Application, have a TextBlock and two Buttons, the text is displayed TextBlock by clicking
I have a custom textblock that has various text properties for left, right and
I have following XAML: <TextBlock HorizontalAlignment=Center VerticalAlignment=Center FontSize=10 FontFamily=Arial Foreground=#414141> <Run Text={Binding LoadsCount} />
I want to bind a user control (View) to a ListBoxItem. The ListBox is

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.