zoukankan      html  css  js  c++  java
  • WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

          WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 

    故事背景:

      需要检索某目录下文件,并列出来,提供选择和其他功能。

     

    第一版需求:

      列出文件供选择即可,代码如下:

          

    <ListBox Name="multiSelectFileLst" ItemsSource="{Binding FileList}" Grid.Row="1" Grid.Column="1" >
                        <ListBox.ItemTemplate >
                            <DataTemplate >
                                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
                                    <Border Margin="5,1,5,1" Padding="3" BorderBrush="Green"  BorderThickness="1" CornerRadius="2">
                                        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="300" ></ColumnDefinition>
                                                <ColumnDefinition Width="Auto" ></ColumnDefinition>
                                                <ColumnDefinition Width="80" ></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
    
                                            <CheckBox Content="{Binding FileName}" ToolTip="{Binding FileName}" IsChecked="{Binding IsSelected}"/>
                                            <Button Content="查看" Width="75" HorizontalAlignment="Left" Grid.Column="2" 
                                                    Command="{Binding DataContext.OpenFileCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}" CommandParameter="{Binding ExcelFilePath}"  />
                                            
                                        </Grid>
                                    </Border>
                                </Grid>
                                    
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

    可取之处:

      使用Border 画的边框还不错。

    第二版需求:

      需要添加文件的目录一项,因此要添加列标题,该用ListView实现,改动较小:

      

    <ListView Name="multiSelectFileLst" ItemsSource="{Binding FileList}" Grid.Row="1" Grid.Column="1" >
                            <ListView.View>
                                <GridView >
                                    <GridViewColumn Header="文件名">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <CheckBox Content="{Binding FileName}" ToolTip="{Binding FileName}" IsChecked="{Binding IsSelected}"/>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Header="文件目录">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding FolderPath}" ToolTip="{Binding FolderPath}" Foreground="Black" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Header="查看文件">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <Button Content="查看" Width="75" HorizontalAlignment="Left"
                                                        Command="{Binding DataContext.OpenFileCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}" CommandParameter="{Binding ExcelFilePath}"  />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>                        
                        </ListView>

    最后,个人觉得默认样式不好看,改为使用actiprosoftware的ThemedDataGrid:

      

    <datagrid:ThemedDataGrid Name="multiSelectFileLst" ItemsSource="{Binding FileList}" Grid.Row="1" Grid.Column="1"
                                                 AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeRows="False" CanUserDeleteRows="False" 
                                                 AreRowDetailsFrozen="False" CanUserSortColumns="False" CanUserReorderColumns="False"
                                                 HeadersVisibility="Column" SelectionUnit="FullRow" 
                                                 >
    
                            <datagrid:ThemedDataGrid.Columns>
                                <DataGridTemplateColumn Header="文件名" IsReadOnly="True" Width="auto">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox Content="{Binding FileName}" ToolTip="{Binding FileName}" IsChecked="{Binding IsSelected}" Margin="0,0,20,0" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >
                                <DataGridTemplateColumn Header="文件目录" IsReadOnly="True" Width="auto" >
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding FolderPath}" ToolTip="{Binding FolderPath}" Margin="20,0,20,0" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >
                                <DataGridTemplateColumn Header="查看文件" IsReadOnly="True" Width="80" >
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Content="查看" HorizontalAlignment="Left" Width="75"
                                                    Command="{Binding DataContext.OpenFileCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}" 
                                                    CommandParameter="{Binding ExcelFilePath}"  />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn >
                            </datagrid:ThemedDataGrid.Columns>
                        </datagrid:ThemedDataGrid>

    其中存在疑问:ListBox中的模板长度设置,没有随界面缩放,后续解决了补上。

      

    严以律己、宽以待人
  • 相关阅读:
    连通最大子数组和(结对开发)
    第五周学习进度情况
    敏捷开发方法综述
    第四周学习进度情况
    环形数组最大子数组之和
    第四次程序(结对开发)
    第三周学习进度情况
    第三次程序—四则运算(结对开发)
    构建之法阅读笔记02
    按照Right-BICEP要求对实验二进行测试
  • 原文地址:https://www.cnblogs.com/kuang17/p/4776532.html
Copyright © 2011-2022 走看看