1

我要疯了,试图弄清楚这一点。我有一个 DockPanel,其中一些东西停靠在 Top 上,还有一个 ItemsControl 作为它的中心内容(它表现为一个包含更多 DockPanel 的 WrapPanel)。

我希望中心 ItemsControl 根据需要扩展父 DockPanel 的宽度。我不希望停靠在 DockPanel 顶部的东西导致 DockPanel 展开,但我希望它使用 ItemsControl 的宽度所请求的任何空间。

下面是一些非常简化的 XAML,仍然可以捕获行为:

<Window x:Class="SizingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="Width" Height="150">
    <Grid>
        <DockPanel>
            <TextBlock DockPanel.Dock="Top">Here's some really long text that should not force the window to expand. Just clip if it's wider than the buttons.</TextBlock>
            <WrapPanel Orientation="Vertical">
                <Button>Button 1</Button>
                <Button>Button 2</Button>
                <Button>Button 3</Button>
                <Button>Button 4</Button>
                <Button>Button 5</Button>
                <Button>Button 6</Button>
                <Button>Button 7</Button>
                <Button>Button 8</Button>
                <Button>Button 9</Button>
                <Button>Button 10</Button>
            </WrapPanel>
        </DockPanel>
    </Grid>
</Window>

换句话说,WrapPanel/DockPanel/Grid/Window 应该调整它们的宽度以适应 WrapPanel 的按钮列,但我希望在用完 WrapPanel 请求的所有可用空间后简单地裁剪 TextBlock。调整窗口高度(这会导致 WrapPanel 调整自身并添加/删除列)应该会导致 TextBlock 更改宽度 - 和剪辑 - 以匹配 WrapPanel。我该如何做?

4

2 回答 2

2

您只需要进行一些更改。首先,您需要WrapPanel跨越整个Width. 然后,您可以将属性数据绑定TextBlock.WidthWrapPanel.ActualWidth属性。最后,您还需要将HorizontalAlignment属性设置为Lefton TextBlock。这应该可以解决问题:

<Grid>
    <DockPanel>
        <TextBlock DockPanel.Dock="Top" HorizontalAlignment="Left" 
            Width="{Binding ActualWidth, ElementName=Panel}" Text="Here's some really 
            long text that should not force the window to expand..." />
        <WrapPanel Name="Panel" Orientation="Vertical" HorizontalAlignment="Left">
            <Button>Button 1</Button>
            <Button>Button 2</Button>
            <Button>Button 3</Button>
            <Button>Button 4</Button>
            <Button>Button 5</Button>
            <Button>Button 6</Button>
            <Button>Button 7</Button>
            <Button>Button 8</Button>
            <Button>Button 9</Button>
            <Button>Button 10</Button>
        </WrapPanel>
    </DockPanel>
</Grid>

最后要注意的是,如果您使用该TextTrimming属性在文本被截断之前在文本末尾添加省略号 (...) 会更好看。你可以这样做:

<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Left" Width="{Binding ActualWidth,
    ElementName=Panel}" Text="Here's some really long text that should not force the 
    window to expand..." TextTrimming="CharacterEllipsis" />

在此处输入图像描述

于 2014-03-10T21:00:16.400 回答
0

你可以看看这个,也许你会在那里找到解决问题的方法:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <DockPanel LastChildFill="False">
            <TextBlock DockPanel.Dock="Top">
                Here's some really long text that should not force the window to expand. Just clip if it's wider than the buttons.
            </TextBlock>
            <WrapPanel x:Name="wrapPanel" DockPanel.Dock="Left" Orientation="Vertical">
                <Button>Button 1</Button>
                <Button>Button 2</Button>
                <Button>Button 3</Button>
                <Button>Button 4</Button>
                <Button>Button 5</Button>
                <Button>Button 6</Button>
                <Button>Button 7</Button>
                <Button>Button 8</Button>
                <Button>Button 9</Button>
                <Button>Button 10</Button>
            </WrapPanel>
        </DockPanel>
    </Grid>
</Window>

告诉 DockPanel 不要用最后一个孩子填充其可用空间,以防止 WrapPanel 拉伸。最后告诉窗口采用 WrapPanel 的计算宽度,如下所示:

public MainWindow()
{
    InitializeComponent();
    this.wrapPanel.Loaded += wrapPanel_Loaded;
}

void wrapPanel_Loaded(object sender, RoutedEventArgs e)
{
    this.Width = this.wrapPanel.ActualWidth + 20;
}

就是这样。文本将被剪掉,但当您调整窗口大小时,文本将计算新的剪裁。

于 2014-03-10T21:14:45.247 回答