WPF中DataGrid使用初步
(1)自动生成列
<DataGrid AutoGenerateColumns="True" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick" />
(2)取消自动生成列,手动绑定到相应字段
<DataGrid AutoGenerateColumns="False" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Header="公司" Binding="{Binding CompanyName}"></DataGridTextColumn>
<DataGridTextColumn Header="固定资产" Binding="{Binding FixedAssets}" Width ="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
最后一列设置Width ="*"是为了取消空白列。
对应的后台代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
datagrid.ItemsSource = AccessDAL.OleDbHelper.ExecuteDataTable("SELECT * from Customers").DefaultView;
}
//双击DataGrid,显示相应信息
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView row = datagrid.SelectedItem as DataRowView;
MessageBox.Show(row["id"].ToString());
}
//如果绑定到对象集合,如ObservableCollection<Employee>,代码如下:
ObservableCollection<Employee> col;
public EmployeeManage()
{
InitializeComponent();
col = new ObservableCollection<Employee>();
col.Add(new Employee() { Id = 1, Name = "Jim", Salary = 2500.50f });
col.Add(new Employee() { Id = 2, Name = "John", Salary = 2600.50f });
datagrid.ItemsSource = col;
}
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Employee emp=datagrid.SelectedItem as Employee;
MessageBox.Show(emp.Id.ToString());
}
(3)删除选中的多行数据
private void Delete_Click(object sender, RoutedEventArgs e)
{
for (int i = datagrid.SelectedItems.Count - 1; i >= 0; i--)
{
Good good = datagrid.SelectedItems[i] as Good;
goods.Remove(good);
}
}
(4)控制DataGrid是否显示滚动条
<DataGrid AutoGenerateColumns="True" Name="datagrid" CanUserAddRows="False" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden" />
(5)设置DataGrid 鼠标移动改变鼠标所在行颜色样式
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="LightBlue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
(6)获取选中的当前行某列值
方法一:
DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem;
string result = mySelectedElement.Row[0]ToString();
方法二:
var a = this.dataGrid1.SelectedItem;
var b = a as DataRowView;
string result = b.Row[0].ToString();
(7)模版列使用
<DataGrid Margin="0,39,35,173" Name="dataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False" AlternatingRowBackground="Bisque" CellEditEnding="dgCust_CellEditEnding" RowEditEnding="dgCust_RowEditEnding"
Background="#88AEE9" SelectionChanged="dgCust_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=menu_item}" IsReadOnly="True"
Header="Item" Width="100" />
<DataGridTemplateColumn Header="Image" Width="80" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="40" Width="40" Source="{Binding menu_image}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Edit Row">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit" Click="EditButton_Click" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
(8)设置DataGridTextColumn的文本对齐方式
DataGrid里面存在着像DataGridColumnHeader、DataGrid、DataGridCell等相关控件的样式设置,例如让一个DataGrid里面某一列的控件内容居中显示,
对于DataGridColumnHeader,设置样式如下:
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGrid:DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
普通控件设定样式使用CellStyle,或者属性即可,当设定DataGridTextColumn的文本显示时候,需要设定其ElementStyle,例如:
Style部分:
<Window.Resources>
<ResourceDictionary>
<Style x:Key="dgCell" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
调用部分:
<dg:DataGrid>
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Width="300" Binding="{Binding Path=Wid1}" ElementStyle="{StaticResource dgCell}">
</dg:DataGridTextColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>