WPF:同步ItemsControl中所有项目的宽度

时间:2015-03-11 19:16:34

标签: c# wpf xaml wpf-controls

是否可以将WrapPanel中所有TextBlocks的宽度调整为WrapPanel中最大TextBlock的大小?最终结果应该是控件的宽度包含"一些数据"与包含&#34的控件具有相同的宽度;比以前更多的数据。"我已经附上我的初始代码作为起点。我使用字符串作为示例,但集合数据和模板可以是任何东西,所以我不能依赖字符串的长度。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Collections:ArrayList x:Key="data">
            <System:String>Some data</System:String>
            <System:String>Some more data</System:String>
            <System:String>Even more data than before</System:String>
        </Collections:ArrayList>
    </Window.Resources>
    <ItemsControl ItemsSource="{StaticResource data}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel></WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Margin="5" BorderThickness="1" BorderBrush="Black">
                    <TextBlock Text="{Binding}"></TextBlock>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

所需输出的图像:

Desired output

4 个答案:

答案 0 :(得分:12)

使用共享大小的网格:

<ItemsControl ItemsSource="{StaticResource data}" Grid.IsSharedSizeScope="True">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel></WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="ColumnSize" />
                </Grid.ColumnDefinitions>
                <Border Margin="5" BorderThickness="1" BorderBrush="Black">
                    <TextBlock Text="{Binding}"></TextBlock>
                </Border>
            </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

保证所有列的宽度与共享大小组的宽度相同。由于它们的大小是自动的,因此它们也会调整为任何网格内容的最大实例。

答案 1 :(得分:4)

您必须创建自己的自定义版本的wrappanel,它是从wrappanel派生的,以实现您想要的效果。 通常,对于自定义面板,您必须覆盖以下两种方法:

  • MeasureOverride
  • ArrangeOverride

在你的情况下,你需要measueoverride,你将迭代元素并找出哪一个是最大的,然后对arrangeoverride中的所有元素使用相同的大小。

有关如何创建自定义面板的更多信息: http://www.wpftutorial.net/CustomLayoutPanel.html

答案 2 :(得分:2)

您可以在itemsPanel中添加Horizo​​ntalContentAlignment = Stretch和Add UniformGrid。

 <Window.Resources>
    <Collections:ArrayList x:Key="data">
        <System:String>Some data</System:String>
        <System:String>Some more data</System:String>
        <System:String>Even more data than before</System:String>
    </Collections:ArrayList>
</Window.Resources>
<ListBox ItemsSource="{StaticResource data}" HorizontalContentAlignment="Stretch">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid IsItemsHost="True"></UniformGrid>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Margin="5" BorderThickness="1" BorderBrush="Black" >
                <TextBlock Text="{Binding}" Width="{Binding 
            RelativeSource={RelativeSource TemplatedParent},
            Path=Width}"></TextBlock>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

答案 3 :(得分:1)

这是Philip Stuyck的自定义面板建议的替代方案,您可能会发现您的特定方案更直接。 (诚​​然,这是一个黑客攻击。)

您可以使用FormattedText类计算字符串的长度。因此,您可以迭代字符串并计算最大长度(这也假设您知道字体系列和大小)。然后将文本块的宽度绑定到最大宽度。我将宽度值存储在父级别的单个属性中,然后使用RelativeSource绑定:

<TextBlock Text="{Binding}" 
           Width="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
                           Path=MaximumWidth}}" 
       />

(这种方法的一个缺点是,如果项目集合发生变化,则必须重新计算MaximumWidth。)

相关问题