代码之家  ›  专栏  ›  技术社区  ›  Andreas

使用WPF创建类似于Windows资源管理器的GUI

  •  2
  • Andreas  · 技术社区  · 16 年前

    在WinForms中,我会使用一个ListView来完成这个任务,但是在WPF中,唯一接近的是一个带有自定义ControlTemplate的listbox,但是看起来太费劲了!

    4 回复  |  直到 16 年前
        1
  •  2
  •   Jobi Joy    16 年前

    HeirarchichalDataTemplate 为文件系统类定义

        2
  •  1
  •   Nir    16 年前

    首先,我们的数据对象:

    public class Item
    {
        public string Type { get; set; }
        public string Name { get; set; }
        public ImageSource Icon { get; set; }
    }
    

    在代码中创建它们,并将它们的列表设置为以下窗口的DataContext:

    <Window x:Class="ListViewTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Name="W">
        <Window.Resources>
            <CollectionViewSource x:Key="Items" Source="{Binding}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Type"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
            <DataTemplate x:Key="ItemTemplate">
                <Grid Width="128">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40"/>
                        <RowDefinition Height="12"/>
                    </Grid.RowDefinitions>
                    <Image Source="{Binding Icon}"/>
                    <TextBlock Text="{Binding Name}" Grid.Row="1"/>
                </Grid>
            </DataTemplate>
            <ItemsPanelTemplate x:Key="ItemPanel">
                <WrapPanel Orientation="Horizontal" Width="{Binding ElementName=W, Path=ActualWidth}"/>
            </ItemsPanelTemplate>
            <DataTemplate x:Key="HeaderTemplate">
                <StackPanel Margin="0 15">
                    <TextBlock Text="{Binding Name}"/>
                    <Rectangle Height="1" Fill="Blue"/>
                </StackPanel>
            </DataTemplate>
            <Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander Header="{Binding Name}" IsExpanded="True">
                                <ItemsPresenter/>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
    
        <Grid>
            <ListBox 
                ItemsSource="{Binding Source={StaticResource Items}}"
                ItemTemplate="{StaticResource ItemTemplate}"
                ItemsPanel="{StaticResource ItemPanel}">
                <ListBox.GroupStyle>
                    <GroupStyle 
                        HeaderTemplate="{StaticResource HeaderTemplate}"
                        ContainerStyle="{StaticResource ContainerStyle}"/>
                </ListBox.GroupStyle>
            </ListBox>
        </Grid>
    </Window>
    

        3
  •  0
  •       16 年前

    从中查看FolderView、FileView等控件 Shell MegaPack.WPF

    它支持分组和图标模式(以及许多其他方式)。