WPF用户控件中的自动高度

时间:2013-03-20 15:20:55

标签: wpf xaml layout

假设我有以下XAML。

如何实现最外层的Button具有这样的高度,以便来自TextBlock的文字完全可见?

<StackPanel>
        <Button HorizontalContentAlignment="Stretch" Width="100" >
            <DockPanel >
                <CheckBox DockPanel.Dock="Left"/>
                <Button DockPanel.Dock="Right" Content="X" />                
                <TextBlock >
  some very long text some very long textsome very long textsome very long textsome very long text
                </TextBlock>
            </DockPanel>
        </Button>
    </StackPanel>

2 个答案:

答案 0 :(得分:3)

TextBlock.TextWrapping设置为Wrap,以便在文本到达允许空格的边缘时将文本换行到新行

<TextBlock TextWrapping="Wrap" 
           Text="some very long text some very long textsome very long textsome very long textsome very long text" />

答案 1 :(得分:0)

添加另一个堆栈面板

<StackPanel>
        <Button HorizontalContentAlignment="Stretch" Width="auto" >
            <DockPanel>
                <CheckBox DockPanel.Dock="Left"/>
                <StackPanel>
                    <Button DockPanel.Dock="Right" Content="x" HorizontalAlignment="Right" />
                    <TextBlock Text="some very long text some very long textsome very long textsome very long textsome very long text" />
                </StackPanel>
            </DockPanel>
        </Button>
    </StackPanel>