<!-- DataGrid列标题样式 -->
<Style x:Key="style_DgCloumnHeader" TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Orange" />
<Setter Property="Foreground" Value="White" />
</Style>
///Xaml中调用
ColumnHeaderStyle="{DynamicResource style_DgCloumnHeader}"
初步分析:应该是初始化的时候将格式重新覆盖了,具体什么哪里被覆盖了尚需再另外分析。
目前尚未找到具体根据,采用临时方案,将格式单独设置,不采用绑定方法。
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid.ColumnHeaderStyle>
<!-- 注意,样式设置需要防御Columns标签内,不然会报错“在使用 ItemsSource 之前,项集合必须为空” -->
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static res:StringResource.NO}" Binding="{Binding ID,StringFormat={}{0:D}}"/>
<DataGridTextColumn Header="{x:Static res:StringResource.Type}" Binding="{Binding EventType}"/>
<DataGridTextColumn Header="{x:Static res:StringResource.RawDataID}" Binding="{Binding RawDataID,StringFormat={}{0:D}}"/>
<DataGridTextColumn Header="{x:Static res:StringResource.Message}" Binding="{Binding Message}"/>
<DataGridTextColumn Header="{x:Static res:StringResource.UserName}" Binding="{Binding UserName }"/>
<DataGridTextColumn Header="{x:Static res:StringResource.Date}" Binding="{Binding EventTime,StringFormat={}dd/MM/yyyy HH:mm:ss}"/>
</DataGrid.Columns>
SelectionMode属性:
当SelectionMode属性为Extended时,可通过设置SelectionUint属性设置更多的选中模式
private void Dg_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//识别是否有数据存在
if (dg_list.SelectedCells[0].Column != null)
{
//所选内容非空时
if (dg_list.SelectedCells[0].Column.GetCellContent(dg_list.SelectedItem) != null)
{
selectedChannel.ID = Convert.ToInt32((dg_list.SelectedCells[0].Column.GetCellContent(dg_list.SelectedItem) as TextBlock).Text, FormatInfo.Number);
selectedChannel.Name = (dg_list.SelectedCells[1].Column.GetCellContent(dg_list.SelectedItem) as TextBlock).Text;
selectedChannel.Notes = (dg_list.SelectedCells[2].Column.GetCellContent(dg_list.SelectedItem) as TextBlock).Text;
}
}
}
说明:
//识别是否有数据存在
if (dg_UserList.SelectedItem != null)
{
Int32 id = Convert.ToInt32(((DataRowView)dg_UserList.SelectedItem).Row["ID"], FormatInfo.Number);
selectedUser = PGxMySqlDB.GetUser(id);
}
将类型转换成DataRowView,然后通过行列获取数据。
//设置默认选中第一个主项目
if (dg_ItemTypeList.Items.Count > 0)
{
dg_ItemTypeList.UpdateLayout();
dg_ItemTypeList.ScrollIntoView(dg_ItemTypeList.Items[0]); //若不设置,则所选行超出界面时会报错
DataGridRow row = (DataGridRow)dg_ItemTypeList.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
}
updateLayout()方法是关键,没有调用该方法无法正常选中。
实际使用过程中,将DataGrid控件放置于TabControl控件中时会发生ContainerFromIndex(0)返回值为null的异常
DataGridRow row = (DataGridRow)dg_UserList.ItemContainerGenerator.ContainerFromIndex(0);
后面经过确认排查,确认时由于控件未进行完整加载完毕就调用所以返回值未null。
加载完毕后通过方法调用可以正常返回。
针对初始化问题,改用调用TabItem的GetFocus事件,再进行调用就可以正常返回。
xaml文件设置
<DataGridCheckBoxColumn Binding="{Binding IsChecked}">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="chk_SampleExtractionList_AllSelect" Click="Chk_SampleExtractionList_AllSelect_Click"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
后台设置
private void Chk_SampleReceiveList_AllSelect_Click(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
if (checkBox.IsChecked == true)
{
for (int i = 0; i < dt_SampleReceive.Rows.Count; i++)
{
dt_SampleReceive.Rows[i]["IsChecked"] = true;
}
}
else
{
for (int i = 0; i < dt_SampleReceive.Rows.Count; i++)
{
dt_SampleReceive.Rows[i]["IsChecked"] = false;
}
}
}
事件:LoadingRow事件
获取行数据:
DataRow row = (e.Row.Item as DataRowView).Row;
获取行号:
index = e.Row.GetIndex();