0

I have the following code:

<Window.Resources>
    <DataTemplate x:Key="ParameterItemTemplate">
        <my:ParameterItem ParamValue="{Binding Value}" Description="{Binding Name}"/>
    </DataTemplate>
</Window.Resources>

<Grid Width="Auto">
    <GroupBox BorderBrush="Black" 
              BorderThickness="2" 
              Width="Auto" 
              HorizontalAlignment="Left" 
              VerticalAlignment="Top">
        <GroupBox.HeaderTemplate>
            <DataTemplate>
                <Button Content="Header" 
                        Width="{Binding RelativeSource={RelativeSource Self}, Path=Width}" 
                        Height="30">
                </Button>
            </DataTemplate>
        </GroupBox.HeaderTemplate>
        <ScrollViewer HorizontalScrollBarVisibility="Auto">
            <ItemsControl x:Name="Test" 
                          ItemsSource="{Binding Items}" 
                          ItemTemplate="{StaticResource ParameterItemTemplate}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical" Height="228"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
    </GroupBox>

</Grid>

When the binded items fill my ItemsControl, the Button placed in the Header of the GroupBox does not change it's width. Do I have a binding problem?
The width of the button fits only it's content.

Levon Alikhanov
  • 103
  • 4
  • 13

1 Answers1

3

What do you expect if you bind the width with itself? Try

<Button Content="Header" 
        Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type GroupBox}}, Path=ActualWidth}" 
        Height="30">
LPL
  • 16,827
  • 6
  • 51
  • 95
  • Thanks, the only problem that remains is that I can't see the right part of my border. – Levon Alikhanov May 13 '13 at 12:47
  • This is more difficult. To solve this problem I think you have to retemplate the GroupBox to stretch the content. See [GroupBox Header Customization](http://stackoverflow.com/q/4235252/620360) to start. In your case maybe it's simpler to use a Converter and subtract an amount from the ActualWidth. – LPL May 13 '13 at 13:32
  • The trick with Binding to ActualWidth is described here: http://stackoverflow.com/questions/22981622/wpf-groupbox-header-alignment – Daniel Albuschat Jul 06 '15 at 07:31