每个组的不同视图模式

时间:2018-01-13 14:08:25

标签: c# wpf listview

我将数据项组织成组,并希望一次显示所有组。每个组都指定是否必须显示为

是否可以为一个ListView中的每个组选择查看模式?或者我应该为每个组创建不同的ListView?或者使用ListBox和Icons视图将作为一个项目嵌入到wrappanel?

Group 1
------------------------------
Item11  Item12  Item13  Item14
Item15  Item16

Group 2
------------------------------
Item21
Item22
Item23
Item24

Group 3
------------------------------
Item31  Item32  Item33  Item34
Item35  Item36

2 个答案:

答案 0 :(得分:1)

MainViewModel代码:

<Window x:Class="Question48240657.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Question48240657"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Group1}">
            <TabControl ItemsSource="{Binding DataList}" DisplayMemberPath="Name"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Group2}">
            <ListBox ItemsSource="{Binding DataList}" DisplayMemberPath="Name"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Group3}">
            <ItemsControl ItemsSource="{Binding DataList}" DisplayMemberPath="Name">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ItemsControl ItemsSource="{Binding Groups}"/>
    </Grid>
</Window>

Xaml代码:

$array[0][0] = 5;
$array[0][1] = 115;
$array[0][2] = 320;
$array[1][0] = 5;
$array[1][1] = 100;
$array[1][2] = 115;

//Orginize the array
$tempValues = array();
foreach ( $array as $key1 => $value1 ) {
    foreach ( $value1 as $key2 => $value2 ) {
        if ( !isset( $tempValues[ $value2 ] ) ) $tempValues[ $value2 ] = array();
        $tempValues[ $value2 ][] = array( $key1, $key2 );
    }
}

//Check which has 2/more
$duplicateValues = array();
foreach ( $tempValues as $key => $tempValue ) {
    if ( count( $tempValue ) >= 2 ) $duplicateValues[ $key ] = $tempValue;
}

echo "<pre>";
print_r( $duplicateValues );
echo "</pre>";

示例图片

enter image description here

答案 1 :(得分:0)

您可以使用DataTemplate执行此操作并设置DataType属性。

示例:

>>> x[0]
'admin'
>>> x[1]
'passw'
>>> x[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range