Is there a SharedSizeGroup equivalent for ListViews inside ItemsControl?

573 views Asked by At

I use an ItemsControl representing Countries. For each country I use a ListView for showing its cities:

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

The result:

enter image description here

I need that whenever the user changes a column width in the first listview the second one adjust its width accordingly (something like SharedGroupSize for grids).

How can I accomplish this?

1

There are 1 answers

0
Gregor Stamač On

Create a custom UserControl which has a dependency property for each column width. Then two-way bind column widths to them.

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

and code behind

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); }
    }

    .......
}