Grid in ItemTemplate does not stretch to available width?

3 posts, 1 answers
  1. Sam
    Sam avatar
    67 posts
    Member since:
    Jun 2011

    Posted 30 May 2012 Link to this post

    Hi there, 

    I've got a RadTreeView along these lines:

    <telerik:RadTreeView
        x:Name="treeview" Background="Transparent" Margin="0, 6, 0, 0" BorderThickness="0"
        Grid.Row="1" IsEditable="True" IsTextSearchEnabled="True" BringIntoViewMode="HeaderAndItems"
        ItemsSource="{Binding Items}"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
                     
        <!-- Item Template -->
        <telerik:RadTreeView.ItemTemplate>
            <telerik:HierarchicalDataTemplate ItemsSource="{Binding ChildItems}">
                <telerik:ContainerBinding.ContainerBindings>
                    <!-- Used to bind the treeview item to the data item for item tracking -->
                    <telerik:ContainerBindingCollection x:Name="BindingsCollection">
                        <telerik:ContainerBinding
    PropertyName="IsInEditMode"
    Binding="{Binding IsInEditMode, Mode=TwoWay}" />
                    </telerik:ContainerBindingCollection>
                </telerik:ContainerBinding.ContainerBindings>
     
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
     
                    <TextBlock Text="{Binding Name}" />
     
                    <TextBlock Grid.Column="1" Margin="10, 0, 0, 0" Text="{Binding InstrumentCount}" />
                </Grid>                                      
            </telerik:HierarchicalDataTemplate>
        </telerik:RadTreeView.ItemTemplate>
    </telerik:RadTreeView>


    The tree is inside a grid column that also has an expander and a splitter associated with it, so the tree width itself expands to fit the available column space.

    The item template is meant to show a name, then on the right hand side of the tree view, show a number.

    The problem I'm having is that the Grid inside the HierarchicalDataTemplate does not expand to fill the available width, even if I explicitly set HorizontalAlignment to Stretch (which is the default anyway), so I don't get the numbers right-aligned as required.

    I can't use a fixed width either.

    Is there any way to get the Grid inside the ItemTemplate to take up all available width on the control, as the focus and selected visuals do, or is this not possible?

    Thanks,

    Sam




  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 04 Jun 2012 Link to this post

    Hi Sam,

    You'll need to set the RadTreeViewItems HorizontalContentAlignment to Stretch like so:
    <telerik:RadTreeView.ItemContainerStyle>
      <Style TargetType="telerik:RadTreeViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      </Style>
    </telerik:RadTreeView.ItemContainerStyle>

    Give this a try and let us know if it helps or if we can further assist you.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Sam
    Sam avatar
    67 posts
    Member since:
    Jun 2011

    Posted 04 Jun 2012 Link to this post

    Hi Tina, that works like a charm! Thanks for providing a solution.

    Cheers,

    Sam
Back to Top