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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:06:09+00:00 2026-06-06T21:06:09+00:00

I have TreeView , which displays some data using data templates. Here’s XAML: <TreeView

  • 0

I have TreeView, which displays some data using data templates. Here’s XAML:

<TreeView Grid.Row="0" ItemsSource="{Binding Railways}" x:Name="tvDatawareObjects"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.Resources>
        <!-- other templates here... -->
        <HierarchicalDataTemplate DataType="{x:Type viewModels:ProjectViewModel}" ItemsSource="{Binding Phases}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Model.Code}" FontWeight="DemiBold" />
                <TextBlock Text="{Binding Model.Title}" TextWrapping="Wrap" Foreground="Gray" Grid.Row="1" />
            </Grid>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type viewModels:CollectionViewModel}" ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding CollectionName}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

Text wrapping for <TextBlock Text="{Binding Model.Title}" TextWrapping="Wrap" Foreground="Gray" Grid.Row="1" /> doesn’t work. What am I doing wrong?

  • 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-06T21:06:10+00:00Added an answer on June 6, 2026 at 9:06 pm

    I did it. 🙂

    Accordind to the link, provided in the Dan’s answer, the reason is lying in the default TreeViewItemTemplate.

    Unfortunately, simple binding to TreeView.ActualWidth can’t help. This is because width of each item is less than TreeView.ActualWidth by definition – items are rendered with some horizontal offset, depending on their level.

    Hence, to solve the problem, I need to calculate width of item like this:

    width = actual_width_of_tree_view - relative_horizontal_offset_of_item
    

    To be more precise, I need ScrollViewer.ViewportWidth, so as TreeViewContent might be scrolled vertically, and visible area of TreeView will be less than TreeView.ActualWidth in this case.

    Here’s attached property:

        public static Double GetProjectTitleWidth(DependencyObject obj)
        {
            return (Double)obj.GetValue(ProjectTitleWidthProperty);
        }
    
        public static void SetProjectTitleWidth(DependencyObject obj, Double value)
        {
            obj.SetValue(ProjectTitleWidthProperty, value);
        }
    
        public static readonly DependencyProperty ProjectTitleWidthProperty = DependencyProperty.RegisterAttached(
            "ProjectTitleWidth", 
            typeof(Double), 
            typeof(DatawareSearchView),
            new UIPropertyMetadata(0.0, ProjectTitleWidthChanged));
    
        private static void ProjectTitleWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var targetElement = d as FrameworkElement;
            if (targetElement != null)
            {
                var bindingExpr = targetElement.GetBindingExpression(ProjectTitleWidthProperty);
                var sourceElement = bindingExpr.DataItem as FrameworkElement;
                if (sourceElement != null)
                {
                    // calculating relative offset
                    var leftTop = targetElement.TranslatePoint(new Point(0.0, 0.0), sourceElement);
    
                    // trying to find ScrollViewer
                    var border = VisualTreeHelper.GetChild(sourceElement, 0);
                    if (border != null)
                    {
                        var scrollViewer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
                        if (scrollViewer != null)
                        {
                            // setting width of target element
                            targetElement.Width = scrollViewer.ViewportWidth - leftTop.X;
                        }
                    }
                }
            }
        }
    

    …and markup:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <TextBlock Text="{Binding Model.Code}" FontWeight="DemiBold" />
        <TextBlock Text="{Binding Model.Title}" TextWrapping="Wrap" Foreground="Gray" x:Name="tbTitle" Grid.Row="1"
                   localviews:DatawareSearchView.ProjectTitleWidth="{Binding RelativeSource={RelativeSource AncestorType=TreeView}, Path=ActualWidth}"/>
    </Grid>
    

    Of course, provided solution isn’t universal – it assumes, that TreeView has Border and ScrollViewer.

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

Sidebar

Related Questions

I currently have a treeview that displays data using a hierarchicaldatatemplate and when the
Ok, I have a treeview which I am using to display a number of
I have a TreeView control which displays two things: 1) Folder 2) Item Where
I have a WPF TreeView which displays my ViewModel. I have a button that
I have a TreeView which uses a HierarchicalDataTemplate to bind its data. It looks
I have a treeview which contains, per node, a key and text. However, there
I have a treeview (winforms) which have different item types on it. I have
I have a TreeView that is bound to a XmlDataSource control. I've added some
I have a treeView , and i want to know if some node can
i am using jqgrid treeview and i am passing back json response which works

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.