WPF开发中遇到的新知识 -- 6
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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库