user8630414
user8630414

Reputation: 61

How can I give background color here in treeview in complete width?

I have tried with HorizontalAlignment="Stretch" attribute in the stack panel, which is also not doing it. The only Way I can do it now by giving a fixed Width of stack panel but I don't want that.

How can I set the with of this treeview templete's background the whole window streched?

enter image description here

<HierarchicalDataTemplate DataType="{x:Type local:Data}" ItemsSource="{Binding FamilyTexts}">
                    <StackPanel Orientation="Horizontal" Background="Green">
                        <Image x:Name="img" Source="{Binding Default.imageLocation}" ></Image>
                        <Label Content="{Binding Location}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>

Upvotes: 0

Views: 237

Answers (1)

Keithernet
Keithernet

Reputation: 2509

Update your HierarchicalDataTemplate to use a different layout panel. Instead of a StackPanel which will only size itself to fit its children, try using a DockPanel or a Grid with two columns:

<DockPanel LastChildFill="True">
    <Image ... DockPanel.Dock="Left" />
    <Label ... DockPanel.Dock="Left" />
</DockPanel>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Image ... Grid.Column="0" />
    <Label ... Grid.Column="1" />
</Grid>

Also, the default control template for the TreeViewItem limits the width of the header. So you'll need to modify the control template to fix it. This SO question has the answers you need:

HorizontalAlignment="Stretch" not working in TreeViewItem

Upvotes: 1

Related Questions