【WPF】WPF ListView GridView 操作

wpf 这方面首先就是界面代码 直接上代码

GridView 样式分为表格头样式和表格Item样式。

表格头类:GridViewColumnHeader

表格Item类:ListViewItem,GridViewRowPresenter

GridViewColumn 用了 DisplayMemberBinding  ,使得CellTemplate失效。 两者的这能选一个,DisplayMemberBinding  权重大于CellTemplate

 效果如下:

 

xaml代码如下:

    <!--List -->
    <Style TargetType="{x:Type ListView}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Padding" Value="10" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type  ListView}" >
                    <Border Name="Border" BorderThickness="1" Background="White"  >
                        <Border.BorderBrush>
                            <SolidColorBrush Color="#EFF1F6" />
                        </Border.BorderBrush>
                        <ScrollViewer Style="{StaticResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
<!--列表内容样式-->
    <Style TargetType="{x:Type ListViewItem}" x:Key="ListViewItemContainerStylePatientList">
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="Foreground" Value="#3E4A58"/>
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="Bd"  CornerRadius="4" Margin="0,5" Background="{TemplateBinding Background}"   
                                BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" >
                        <GridViewRowPresenter Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="#F6F6F6"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <!--隔行变色-->
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ItemsControl.AlternationIndex" Value="1" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#F6F8FA" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>

 
        <!--隔行变色-->
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ItemsControl.AlternationIndex" Value="1" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#F5F5F5" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>


<!--列头样式定义-->
   <Style x:Key="ListViewHeaderPatientList" TargetType="{x:Type GridViewColumnHeader}">
       <Setter Property="OverridesDefaultStyle" Value="True"/>
       <Setter Property="FontSize" Value="14" />
 
       <Setter Property="Foreground" Value="#3E4A58"/>
       <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                   <Grid Name="gird" Height="44" Margin="10,0"   >
                       <Grid.Background>
                           <SolidColorBrush Color="White"/>
                       </Grid.Background>
                       <Border Name="bd"   CornerRadius="4" Padding="{TemplateBinding Padding}">
                           <ContentPresenter  HorizontalAlignment="Center" VerticalAlignment="Center"/>
                       </Border>
                       <!--<Thumb Name="PART_HeaderGripper" Template="{StaticResource ThumbColumnHeaderSpliter}" HorizontalAlignment="Right" Margin="0,0,-1,0"/>-->
                   </Grid>
                   <ControlTemplate.Triggers>
                       <Trigger Property="IsMouseOver" Value="True">
                           <Setter TargetName="gird" Property="Background">
                               <Setter.Value>
                                   <SolidColorBrush Color="#F7F6F6"/>
                               </Setter.Value>
                           </Setter>
                       </Trigger>


                       <Trigger Property="IsPressed" Value="True">
                           <Setter TargetName="gird" Property="Background">
                               <Setter.Value>
                                   <SolidColorBrush Color="#F7F6F6"/>
                               </Setter.Value>
                           </Setter>
                       </Trigger>


                   </ControlTemplate.Triggers>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>


 

//上面style只是作为样式,方便运行粘贴。


<ListView Grid.Row="2" x:Name="LVPatients" Margin="36,18,36,0" ItemContainerStyle="{StaticResource ResourceKey=ListViewItemContainerStylePatientList}" 
                              GridViewColumnHeader.Click="LVPatients_Click" SelectionChanged="LVPatients_SelectionChanged"  AlternationCount="2"
                                BorderThickness="0" >

<ListView.View>
                                <GridView x:Name="pList" ColumnHeaderContainerStyle="{StaticResource ResourceKey=ListViewHeaderPatientList}">
                                    <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding NAME}"  Width="100"/>
                                    <GridViewColumn Header="性别" DisplayMemberBinding="{Binding GENDER}" Width="65"/>
                                    <GridViewColumn Header="年龄" DisplayMemberBinding="{Binding AGE}" Width="70"/>
                                    <GridViewColumn Header="住院号/门诊号" DisplayMemberBinding="{Binding PATIENTSNO}" Width="140" />
                                    <GridViewColumn Header="类型" DisplayMemberBinding="{Binding TREATTYPE}" Width="90"/>
                                    <GridViewColumn Header="床号" DisplayMemberBinding="{Binding BEDNO}" Width="80"/>
                                    <GridViewColumn Header="科室" DisplayMemberBinding="{Binding DEPTNAME}" Width="140"/>
                                    <GridViewColumn Header="入院时间" DisplayMemberBinding="{Binding PATIENTSTARTTIME}" Width="215"/>
                                    <GridViewColumn x:Name="GCIN" Header="离线下载" Width="115">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <Button Width="100" Background="Transparent" Height="50" Content="{Binding ISDOWNLOADOFFLINEDATA}"  Click="BtnOffLineData_Click"                                                    BorderThickness="0" FontSize="16" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>
                        </ListView>


//这里面就是需要在 <GridViewColumn>列下面加一个<GridViewColumn.CellTemplate>子模板,然后给一个数据模板 绑定数据
                                            <DataTemplate>
                                                <Button Width="100" Background="Transparent" Height="50" Content="{Binding ISDOWNLOADOFFLINEDATA}"  Click="BtnOffLineData_Click"                                                    BorderThickness="0" FontSize="16" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>

//这里的点击事件就是分别处理每一行的点击事件 操作数据

        //这里就是拿到绑定数据该行对象 继续执行下一步操作
        private void BtnOffLineData_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;      
            EntityPatient player = btn.DataContext as EntityPatient;
           
        }

 

 

posted @ 2022-09-22 17:04  小林野夫  阅读(2038)  评论(0编辑  收藏  举报
原文链接:https://www.cnblogs.com/cdaniu/