c# - WPF 同步 ListBox 项目的大小

标签 c# wpf xaml data-binding listbox

我正在使用以下 xaml:

<ListBox Name="Lst" ItemsSource="{Binding Items}"  HorizontalContentAlignment="Stretch" >            
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="500" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1" BorderBrush="Black" >
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <Label Grid.Row="0" Grid.Column="0" Content="Id" />
                    <Label Grid.Row="0" Grid.Column="1" Content="{Binding Id}" />

                    <Label Grid.Row="1" Grid.Column="0" Content="Name" />
                    <Label Grid.Row="1" Grid.Column="1" Content="{Binding Name}" />
                </Grid>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

和隐藏代码:

public MainWindow()
{
    InitializeComponent();

    DataContext = new
    {
        Items = new object[]
        {
            new {Id = 1, Name = "John"},
            new {Id = 2, Name = "John Smith"},
            new {Id = 3, Name = "Andrew"},
            new {Id = 4, Name = "Andrew Wright"},
            new {Id = 5, Name = "Arthur"},
            new {Id = 6, Name = "Arthur Williams"},
            new {Id = 7, Name = "another name"},
            new {Id = 8, Name = "another name and surname"}
        }
    };
}

现在我得到了这个结果:

enter image description here

正如您所看到的,ListBox 中的项目大小不同,因为 DataContext 的属性名称不同。但我想获得类似尺寸的元素,如下所示:

enter image description here

你能帮我解答这个问题吗?预先感谢!

最佳答案

您应该使用网格列 SharedSizeGroup 属性 ( msdn )。

试试这个:

<ListBox Name="Lst" ItemsSource="{Binding Items}"  HorizontalContentAlignment="Stretch" 
         Grid.IsSharedSizeScope="True">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="500" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1" BorderBrush="Black" >
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition SharedSizeGroup="A" />
                    </Grid.ColumnDefinitions>

                    <Label Grid.Row="0" Grid.Column="0" Content="Id" />
                    <Label Grid.Row="0" Grid.Column="1" Content="{Binding Id}" />

                    <Label Grid.Row="1" Grid.Column="0" Content="Name" />
                    <Label Grid.Row="1" Grid.Column="1" Content="{Binding Name}" />
                </Grid>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

关于c# - WPF 同步 ListBox 项目的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25339748/

相关文章:

c# - 无法将类型 'Microsoft.SolverFoundation.Services.Term' 隐式转换为 'bool'

c# - 无法初始化模拟类的成员

c# - MainWindow 构造函数被调用两次

wpf - 如何将 WPF 控件转换为图像?

c# - 如何将 FlowDocument 导出为 DOC(x) 或 XLS

c# - 组合框的默认文本

javascript - 文本框的 Textchanged 事件不会在 chrome 中触发

c# - 如何用C#.Net和DirectX编写音视频播放引擎(综合分析)?

c# - WPF DataGrid - 将数据项的属性绑定(bind)为 CommandParameter

c# - 无法通过 xaml 中给出的名称引用代码隐藏中的面板