从列表中显示groupdescription的集合作为源

时间:2016-07-25 17:43:39

标签: c# wpf

对于误导性的标题感到抱歉。无论如何,我会试着更好地解释。我想在ListView中的集合中显示集合。但是显示应该使用Expanders来显示内部集合项。所有项目都应显示在相应的列中。其实我做的是这个:

XAML

<CollectionViewSource x:Key="GroupedItems" Source="{Binding Countries}">
 <CollectionViewSource.GroupDescriptions>
     <PropertyGroupDescription PropertyName="Name"/>
 </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
...
<ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}" Name="Playing">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Country" Width="150" DisplayMemberBinding="{Binding Source={x:Static sys:String.Empty}}" />
            <GridViewColumn Header="Leagues">                        
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding Leagues}" Margin="25 0 0 0">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid HorizontalAlignment="Stretch" Background="#FFBCDAEC">
                                        <TextBlock FontSize="18" Padding="5" Text="{Binding Name}"/>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="True">
                                    <Expander.Header>
                                        <TextBlock Text="{Binding Name}" Foreground="Red" FontSize="22" VerticalAlignment="Bottom" />
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

上面的代码在扩展程序中显示了国家/地区的名称,但遗憾的是,我无法将扩展程序列表显示为列表中的国家/地区名称。

我也想为联盟展示扩展器,相应的匹配应出现在相应的列中。在我的ViewModel中,我有一个Country和每个Country>Leagues collection >Matches集合的集合。

请参阅以下课程:

public partial class WinExpander : Window
{
    public WinExpander()
    {
        InitializeComponent();

        this.DataContext = new ViewModel();
    }
} 

public class ViewModel
{
    public List<Country> Countries { get; set; }
    public ViewModel()
    {
        Countries = new List<Country>();
    }
}

public class Country
{
    public string Name { get; set; }
    public List<League> Leagues { get; set; }

    public Country()
    {
        Leagues = new List<League>();
    }
}

public class League
{
    public string Name { get; set; }
    public List<Match> Matches { get; set; }

    public League()
    {
        Matches = new List<Match>();
    }
}

public class Match
{
    public string Name { get; set; }
}

1 个答案:

答案 0 :(得分:0)

Output

  1. Expander用于Matches。要显示MatchesLeagues同步,请同步Leagues扩展器和Matches扩展器。当我们扩展League时,相应的Matches扩展器也会扩展。

  2. 为了获得正确的外观,我们必须从Expander Icon列中移除Match

  3. 要同步LeagueMatch Expanders,请在League中引入名为“IsExpanded”的媒体资源。现在League类必须实现INotifyPropertyChanged

    bool _isExpanded=true;
    public bool IsExpanded
    {
        get { return _isExpanded; }
        set { _isExpanded = value; OnPropertyChanged("IsExpanded"); }
    }
    
  4. ListView标记:

    <ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}" Name="Playing">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Country" Width="150" DisplayMemberBinding="{Binding Source={x:Static sys:String.Empty}}" />
                <GridViewColumn Header="Leagues">
                    <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ItemsControl Margin="25 0 0 0" ItemsSource="{Binding Leagues}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Expander IsExpanded="{Binding IsExpanded, Mode=TwoWay}" Margin="5" >
                                                    <Expander.Header>
                                                        <TextBlock FontSize="18" Padding="5" Text="{Binding Name}"/>
                                                    </Expander.Header>
                                                    <ItemsControl ItemsSource="{Binding Matches}">
                                                        <ItemsControl.ItemTemplate>
                                                            <DataTemplate>
                                                                <TextBlock FontSize="18" Padding="5" Text="{Binding Source={x:Static sys:String.Empty}}"/>
                                                            </DataTemplate>
                                                        </ItemsControl.ItemTemplate>
                                                    </ItemsControl>
                                                </Expander>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Match">
                    <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ItemsControl Margin="25 0 0 0" ItemsSource="{Binding Leagues}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Expander Style="{StaticResource MatchExpanderStyle}" IsExpanded="{Binding IsExpanded, Mode=TwoWay}" Margin="5">                                                
                                                    <Expander.Header>
                                                        <TextBlock Visibility="Hidden" FontSize="18" Padding="5" Text="{Binding Name}"/>
                                                    </Expander.Header>
                                                    <ItemsControl ItemsSource="{Binding Matches}">
                                                        <ItemsControl.ItemTemplate>
                                                            <DataTemplate>
                                                                <TextBlock FontSize="18" Padding="5" Text="{Binding Name}"/>
                                                            </DataTemplate>
                                                        </ItemsControl.ItemTemplate>
                                                    </ItemsControl>
                                                </Expander>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </DataTemplate>
    
                            </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListView.GroupStyle>
            ...
        </ListView.GroupStyle>
    </ListView>
    

    将整件事放在Window.ResourcesApplication.Resources下。我已将名为Visibility的{​​{1}}更改为隐藏在ToggleButton中。一切都是由VS自动生成的。

    HeaderSite

    其他所有内容与旧答案中的相同:How to bind a list of item in CollectionViewSource?