wpf 这方面首先就是界面代码 直接上代码
GridView 样式分为表格头样式和表格Item样式。
表格头类:GridViewColumnHeader
表格Item类:ListViewItem,GridViewRowPresenter
<!--列表内容样式--> <Style TargetType="{x:Type ListViewItem}" x:Key="ListViewItemContainerStylePatientList"> <Setter Property="FontSize" Value="18"/> <Setter Property="Height" Value="60"/> <Setter Property="Foreground" Value="#FF555555"/> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Border x:Name="Bd" 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="#FFCCCCCC"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <!--隔行变色--> <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="18"/> <Setter Property="Foreground" Value="White"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GridViewColumnHeader}"> <Grid Name="gird" Height="44"> <Grid.Background> <SolidColorBrush Color="#FF394053"/> </Grid.Background> <Border Name="bd" 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="#CC536680"/> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="gird" Property="Background"> <Setter.Value> <SolidColorBrush Color="#99536680"/> </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; }
标签:btn,表格,样式,GridView,WPF,ListView From: https://www.cnblogs.com/cdaniu/p/16720017.html