WPF,ListView设置分组

原文:WPF,ListView设置分组

WPF,ListView设置分组

今天遇到一个问题,就是在ListView中设置分组。想了很久在网上早了些资料作出一个例子。

分组字段也可以在后台中定义:
    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(listView1.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Country"); 
            view.GroupDescriptions.Add(groupDescription);
             
            //或者
            var sourceView = new ListCollectionView(Trades);
            var groupDesctripition = new PropertyGroupDescription("Country"); 
            sourceView.GroupDescriptions.Add(groupDesctripition);
            listView1.ItemsSource = sourceView;

 XAML

  <Window.Resources>
        <CollectionViewSource x:Key="trades" Source="{Binding}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Country"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

    </Window.Resources>
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="114,35,0,0" Name="textBox1" VerticalAlignment="Top" Width="211" />
        <ListView Height="224" HorizontalAlignment="Left" Margin="68,81,0,0"
                  Name="listView1" VerticalAlignment="Top" Width="460" ItemsSource="{Binding Source={StaticResource trades}}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding TradeType}" Header="TradeType" Width="100" />
                    <GridViewColumn DisplayMemberBinding="{Binding Units}" Header="Units"  Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding USDAmount, StringFormat=C}" Header="USDAmount"  Width="100"/>
                </GridView>
            </ListView.View>
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True" BorderBrush="#EA1730" BorderThickness="0,0,0,1">
                                            <Expander.Header>
                                                <DockPanel>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="150"/>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                </DockPanel>
                                            </Expander.Header>
                                            <Expander.Content>
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition></RowDefinition>
                                                        <RowDefinition></RowDefinition>
                                                    </Grid.RowDefinitions>
                                                    <ItemsPresenter Grid.Row="0"/>
                                                </Grid>
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>
    </Grid> 
  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ObservableCollection<TradeTest> Trades = new ObservableCollection<TradeTest>();
            Trades.Add(new TradeTest() { TradeType = "Subscription1", Units = 1000, USDAmount = 1000, Country = "UNITED STATES" });
            Trades.Add(new TradeTest() { TradeType = "Subscription2", Units = 2000, USDAmount = 2000, Country = "UNITED STATES" });
            Trades.Add(new TradeTest() { TradeType = "Subscription3", Units = 1500, USDAmount = 3000, Country = "UNITED STATES" });

            Trades.Add(new TradeTest() { TradeType = "Subscription4", Units = 1600, USDAmount = 1000, Country = "UNITED KINGDOM" });
            Trades.Add(new TradeTest() { TradeType = "Subscription5", Units = 3000, USDAmount = 1400, Country = "UNITED KINGDOM" });
            Trades.Add(new TradeTest() { TradeType = "Subscription6", Units = 1040, USDAmount = 1600, Country = "UNITED KINGDOM" });
            Trades.Add(new TradeTest() { TradeType = "Subscription7", Units = 1041, USDAmount = 1601, Country = "UNITED KINGDOM" });
            Trades.Add(new TradeTest() { TradeType = "Subscription8", Units = 1048, USDAmount = 1608, Country = "UNITED KINGDOM" });

            Trades.Add(new TradeTest() { TradeType = "Subscription9", Units = 1500, USDAmount = 3000, Country = "UNITED" });
            Trades.Add(new TradeTest() { TradeType = "Subscription10", Units = 1600, USDAmount = 1000, Country = "UNITED" });
            Trades.Add(new TradeTest() { TradeType = "Subscription11", Units = 3000, USDAmount = 1400, Country = "UNITED" });
            Trades.Add(new TradeTest() { TradeType = "Subscription12", Units = 1040, USDAmount = 1600, Country = "UNITED" });
            this.DataContext = Trades; 
        }
    }
    public class TradeTest
    {
        private string _tradetype;
        private int _units;
        private float _amount;
        private string _country;

        public string TradeType
        {
            get { return _tradetype; }
            set { _tradetype = value; }
        }
        public int Units
        {
            get { return _units; }
            set { _units = value; }
        }
        public float USDAmount
        {
            get { return _amount; }
            set { _amount = value; }
        }
        public string Country
        {
            get { return _country; }
            set { _country = value; }
        }
    } 

 



 

上一篇:2019新版《Java开发淘宝高性能购物车项目实战课程完整版》


下一篇:POJ2393 Yogurt factory (贪心)