我很想解决这个问题。我有一个DockPanel,其中一些东西停靠在Top上,一个ItemsControl作为它的中心内容(它表现为包含更多DockPanels的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的Buttons列,但我希望在耗尽WrapPanel请求的所有可用空间后简单地剪切TextBlock。调整窗口高度(导致WrapPanel自行调整并添加/删除列)会导致TextBlock更改宽度 - 和剪辑 - 以匹配WrapPanel。我该如何做到这一点?
答案 0 :(得分:1)
您只需进行一些更改即可。首先,您需要WrapPanel
到不延伸到整个Width
。然后,您可以将TextBlock.Width
属性数据绑定到WrapPanel.ActualWidth
属性。最后,您还需要在HorizontalAlignment
上将Left
属性设置为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" />
答案 1 :(得分: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;
}
那就是它。当您调整窗口大小时文本将计算新剪辑时,文本将被剪掉。