DataGrid 的简单使用
因为我只需要一个简单的表格展示数据,而操作数据我是放在了Button中,所以我需要关闭 DataGrid 本身自带的一些操作数据的功能,以下都是需要关闭的
- RowHeaderWidth
- AutoGenerateColumns
- CanUserResizeColumns
- CanUserResizeRows
- IsReadOnly
- CanUserAddRows
- CanUserDeleteRows
这个属性可以让表格滚动起来比较自然,默认是按照每行来滚动的,现在是按照像素滚动
- VirtualizingPanel.ScrollUnit
表格需要先定义列,一般我使用的是 DataGridTextColumn
和 DataGridTemplateColumn
这两种,一个是单纯的文本展示,一种是模板,可以在内部填充需要的控件
<DataGrid Grid.Row="1" ItemsSource="{Binding DataList}" Margin="5 0"
VirtualizingPanel.ScrollUnit="Pixel"
RowHeaderWidth="0"
AutoGenerateColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
IsReadOnly="True"
CanUserAddRows="False"
CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="规则名称" Width="80" Binding="{Binding Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTemplateColumn Header="线段名称" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox
BorderThickness="0"
ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
ItemsSource="{Binding ActionRules}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource lineToNameConverter}">
<Binding Path="Line.StartPoint"/>
<Binding Path="Line.EndPoint"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="动作参数" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox
ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
ItemsSource="{Binding ActionRules}"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ActionArgs}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="序号" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox
ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
ItemsSource="{Binding ActionRules}"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Index}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="操作" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="删除"
Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid},Path=SelectedItem}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
标签:需要,滚动,表格,--,知识,DataGrid,WPF
From: https://www.cnblogs.com/huangwenhao1024/p/16727484.html