【问题标题】:Wpf Button Width in GroupBox HeaderGroupBox标题中的Wpf按钮宽度
【发布时间】:2013-05-07 11:44:37
【问题描述】:

我有以下代码:

<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 时,放在GroupBoxHeader 中的Button 不会改变它的宽度。我有绑定问题吗?
按钮的宽度只适合它的内容。

【问题讨论】:

    标签: wpf button binding header width


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,剩下的唯一问题是我看不到边框的右侧部分。
    • 这个比较难。为了解决这个问题,我认为你必须重新模板 GroupBox 来拉伸内容。请参阅GroupBox Header Customization 开始。在您的情况下,使用 Converter 并从 ActualWidth 中减去一个数量可能更简单。
    • 这里描述了绑定到 ActualWidth 的技巧:stackoverflow.com/questions/22981622/…
    猜你喜欢
    • 2012-04-19
    • 2011-05-13
    • 1970-01-01
    • 2011-06-21
    • 2020-03-30
    • 1970-01-01
    • 2017-04-26
    • 2017-01-05
    • 2018-02-09
    相关资源
    最近更新 更多