Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:同步ItemsControl中所有项目的宽度_C#_Wpf_Xaml_Wpf Controls - Fatal编程技术网

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

C# WPF:同步ItemsControl中所有项目的宽度,c#,wpf,xaml,wpf-controls,C#,Wpf,Xaml,Wpf Controls,是否可以将WrapPanel中所有文本块的宽度调整为WrapPanel中最大文本块的大小?最终的结果应该是,包含“某些数据”的控件的宽度与包含“比以前更多数据”的控件的宽度相同。我已附加到初始代码作为起点。我以字符串为例,但是集合数据和模板可以是任何内容,所以我不能依赖字符串的长度 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/

是否可以将WrapPanel中所有文本块的宽度调整为WrapPanel中最大文本块的大小?最终的结果应该是,包含“某些数据”的控件的宽度与包含“比以前更多数据”的控件的宽度相同。我已附加到初始代码作为起点。我以字符串为例,但是集合数据和模板可以是任何内容,所以我不能依赖字符串的长度

<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>

一些数据
更多数据
比以前更多的数据
以及所需输出的图像:


您必须创建自己的自定义版本的wrappanel,该wrappanel是从wrappanel派生的,以实现您想要的功能。 通常,对于自定义配电盘,必须覆盖以下2种方法:

  • 测量超越
  • 安排覆盖
在您的情况下,需要使用measueoverride,在其中迭代元素,找出最大的元素,然后对arrangeoverride中的所有元素使用相同的大小

有关如何创建自定义面板的详细信息:

您可以在itemsPanel中添加HorizontalContentAlignment=Stretch和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>

一些数据
更多数据
比以前更多的数据

这是Philip Stuyck的定制面板建议的替代方案,您可能会发现针对特定场景的定制面板建议更简单。(无可否认,这有点像黑客。)

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

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


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

使用共享大小网格:

<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>


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

UniformGrid,解决了比您预期的更多的问题。IMHO不符合OP的要求。如果他调整窗口大小会怎么样?在这篇文章之前我不知道UniformGrid,谢谢。不幸的是,我必须同意@PhilipStuyck的观点,因为它没有与包裹面板相同的调整大小特性。不幸的是,我无法计算字符串的长度,因为我真正的问题与字符串无关,我只是将它们作为一个例子。我曾考虑过某种祖传的宽度绑定,但希望有一个更优雅的解决方案。然而,知道可以这样做还是很好的。我认为我必须满足于一个定制面板,它具有包裹面板的轻微修改特征,但直到我看到@TobyCrawford提出的答案。这是一个相当优雅的解决方案。这很好地解决了我眼前的问题,而无需编写任何自定义代码。对于这个答案,有没有立即想到的限制?