Nekeniehl
Nekeniehl

Reputation: 1691

SizeToContent.Width and TreeView

I have a grid with three differents controls, two are groupbox, and a third one is a treeview. I don't know the initial width so the Window.SizeToContent is set to "Width" Everything perfectly fits inside the window, the problem came when I extend a node from the TreeView, the Window.Width adjust to the new Width where the treeView fits perfect but the rest of the controls are not that nice.

I would like to keep the "original" width given by the SizeToContent, and get a ScrollBar when I extend the treeViewNode instead changing the whole Window.Width

Here is the xaml (just the main grid and the treeview)

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <!--Attachments-->
    <GroupBox x:Name="gbAttachments" Header="Attachments" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalContentAlignment="Stretch">

        <TreeView x:Name="tvAttachments" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Visible" />

    </GroupBox>
    <!--Attachments-->
</Grid>

I have try with ColumnDefinition.Width = "*", HorizontalContentAlignment, and a lot of others differents ways, I have also tried to put it inside a ScrollbarViewer and nothing seems to work. Always when I extend the node, the whole window is extended with the content of the node (that it is actually quite long). I'm pretty sure that the problem is the Window.SizeToContent = 'Width' but what else I can do for the rest of the controls if I don't know the original width? Any Idead, Thanks in advance!

--EDIT

Well the solution was easier than expected, in the event Window_Loaded I get the gbAttachments.ActualWidth and assign it to the TreeView.MaxWidth Not beatiful, pretty sure it can be done by xaml, but it is working.

Upvotes: 2

Views: 2151

Answers (1)

Aaron Thomas
Aaron Thomas

Reputation: 5281

You could try setting the MaxWidth property of the nodes in your tree:

<Style TargetType="TreeViewItem">
    <Setter Property="MaxWidth" Value="80"></Setter>
</Style>

Alternatively, you can bind this value to something, such as another element for example:

<Style TargetType="TreeViewItem">
    <Setter Property="MaxWidth"
       Value="{Binding ElementName=gbAttachments, Path=ActualWidth}"></Setter>
</Style>

Also, if you're concerned about not needing the window's autosizing to occur after it's initialized, you could try something in code behind so that the autosizing only happens on window initialization:

public MainWindow()
{
    InitializeComponent();

    // auto-size width, and save off value
    this.SizeToContent = System.Windows.SizeToContent.Width;
    var actualWidth = this.ActualWidth;
    // manual-size width
    this.SizeToContent = System.Windows.SizeToContent.Manual;
    // set value to what it was, when auto-sized
    this.Width = actualWidth;
}

Upvotes: 1

Related Questions