06 DataGrid控件

06 DataGrid控件

表示用于在可自定义的网格中显示数据的控件

<!--
ItemsSource     绑定数据
CanUserDeleteRows   能否删除行
HeadersVisibility   标题可访问项
-->
<DataGrid Name="grid1" 
          FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" 
          Width="700" Height="300" Background="AliceBlue"
          AutoGenerateColumns="False"
          CanUserDeleteRows="False"
          CanUserSortColumns="True"
          CanUserResizeColumns="True"
          CanUserResizeRows="True"
          CanUserReorderColumns="True"
          GridLinesVisibility="Horizontal"
          ItemsSource="{Binding datas,ElementName=main}" HeadersVisibility="Column" >
    <!--列标题样式-->
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Foreground" Value="Green"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="FontWeight" Value="Bold"></Setter>
            <!--触发器-->
            <Style.Triggers>
                <Trigger Property="IsMouseOver"  Value="True">
                    <Setter Property="ToolTip" Value="鼠标点击可以排序"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <!--单元格样式-->
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="0,0,0,0"/>
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="FontSize" Value="20"/>
            <!--单元格模板,可以自定义单元格展示效果-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="DataGridCell">
                        <Grid Background="Yellow">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.CellStyle>
    <!--定义表格列-->
    <DataGrid.Columns>
        <!--使用Binding绑定数据中的某个项-->
        <DataGridTextColumn Width="300" Header="姓名" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn Width="50" Header="年龄" Binding="{Binding Path=Age}"/>
        <!--将会渲染为多选框的列-->
        <DataGridCheckBoxColumn Width="200" Header="性别" Binding="{Binding Path=Sex}"/>
        <!--将会渲染为下拉菜单的列-->
        <DataGridComboBoxColumn Width="200" Header="爱好" x:Name="aaa" SelectedItemBinding="{Binding Path=Hobby}">
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=Hobby, ElementName=main}"/>
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
        <!--自定义的列-->
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DockPanel Margin="20">
                        <Button>删除</Button>
                        <Button>编辑</Button>
                    </DockPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

posted @ 2023-12-06 09:43  讨厌敲代码的老郭  阅读(47)  评论(0编辑  收藏  举报