GroupBox标题中的Wpf按钮宽度

时间:2013-05-13 12:29:51

标签: wpf button binding header width

我有以下代码:

<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>

当绑定的项目填满我的ItemsControl时,放置在Button的{​​{1}}中的Header不会改变其宽度。我有绑定问题吗?
按钮的宽度仅适合其内容。

1 个答案:

答案 0 :(得分:2)

如果将宽度与自身绑定,您会有什么期望?尝试

<Button Content="Header" 
        Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type GroupBox}}, Path=ActualWidth}" 
        Height="30">