ItemsControl中是否有ListViews的SharedSizeGroup等效项?

时间:2012-11-29 17:20:06

标签: wpf xaml listview itemscontrol sharedsizegroup

我使用代表国家/地区的ItemsControl。对于每个国家/地区,我使用ListView来显示其城市:

<ItemsControl ItemsSource="{Binding Countries}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel Orientation="Vertical" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ListView Margin="10"
                ItemsSource="{Binding Cities}">
        <ListView.View>
          <GridView>
            <GridViewColumn Width="140"
                            Header="City"
                            DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Width="90"
                            Header="Population"
                            DisplayMemberBinding="{Binding Population}" />
          </GridView>
        </ListView.View>
      </ListView>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

结果:

enter image description here

我需要每当用户更改第一个列表视图中的列宽时,第二个相应地调整其宽度(类似于 SharedGroupSize 的网格)。

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

创建一个自定义UserControl,它具有每个列宽的依赖项属性。然后双向绑定列宽度。

<UserControl x:Class="CountryList" x:Name="countryList">
    <ItemsControl ......
        <GridView>
            <GridViewColumn Width="{Binding ColumnWidth1,Mode=TwoWay, ElementName=countryList}"
                        Header="City"
                        DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Width="{Binding ColumnWidth2,Mode=TwoWay, ElementName=countryList}"
                        Header="Population"
                        DisplayMemberBinding="{Binding Population}" />
        </GridView>
    .......
</UserControl>

和背后的代码

public partial class CountryList : UserControl
{
    public static readonly DependencyProperty ColumnWidth1Property = 
        DependencyProperty.Register("ColumnWidth1", typeof(int), typeof(CountryList),
        new PropertyMetadata(140));

    public int ColumnWidth1
    {
        get { return (int)GetValue(ColumnWidth1Property); }
        set { SetValue(ColumnWidth1Property, value); }
    }

    .......
}