Следующие коды являются окей.
xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="Button1" Height="25" Width="100" Content="Fill Data" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<Button x:Name="Button2" Height="25" Width="100" Content="Focus to Cell" VerticalAlignment="Top" HorizontalAlignment="Center"/>
<Button x:Name="Button3" Height="25" Width="100" Content="Color to Cell" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<DataGrid x:Name="DataGrid1" Width="500" Height="200" SelectionMode="Single" SelectionUnit="Cell">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
</Grid>
</Window>
vb.net
Class MainWindow
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
Dim DataSet1 As System.Data.DataSet = New System.Data.DataSet
Dim DataTable1 As System.Data.DataTable = New System.Data.DataTable
DataTable1.Columns.Add("Name")
DataTable1.Columns.Add("Sur Name")
DataTable1.Columns.Add("Country")
DataTable1.Columns.Add("Gender")
DataTable1.Rows.Add({"Donald", "Trump", "United States", "Male"})
DataTable1.Rows.Add({"Angela", "Merkel", "Germany", "Female"})
DataTable1.Rows.Add({"Theresa", "May", "England", "Female"})
DataSet1.Tables.Add(DataTable1)
DataGrid1.ItemsSource = DataSet1.Tables(0).DefaultView
End Sub
Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
DataGrid1.Focus()
DataGrid1.CurrentCell = New DataGridCellInfo(DataGrid1.Items(1), DataGrid1.Columns(2))
DataGrid1.SelectedCells.Clear()
DataGrid1.SelectedCells.Add(DataGrid1.CurrentCell)
End Sub
Private Sub Button3_Click(sender As Object, e As RoutedEventArgs) Handles Button3.Click
Dim myDataGridRow As DataGridRow = CType(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.Items(1)), DataGridRow)
Dim myDataGridCell As DataGridCell = CType(DataGrid1.Columns(2).GetCellContent(myDataGridRow).Parent, DataGridCell)
myDataGridCell.Background = New SolidColorBrush(CType(ColorConverter.ConvertFromString("#ff0000"), Color))
End Sub
End Class
Пожалуйста, запустите вышеуказанные коды и нажмите Button1, Button2 и Button3 по одномуодин, чтобы увидеть, что все в порядке.
Пожалуйста, поймите, что красный цвет никогда не меняется, даже когда вы нажимаете на ячейку.
Мой вопросздесь;
Некоторые люди говорят, что следующий код не является конфиденциальным.
Итак, знаете ли вы альтернативу следующему коду?
Dim myDataGridRow As DataGridRow = CType(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.Items(1)), DataGridRow)
Dim myDataGridCell As DataGridCell = CType(DataGrid1.Columns(2).GetCellContent(myDataGridRow).Parent, DataGridCell)
myDataGridCell.Background = New SolidColorBrush(CType(ColorConverter.ConvertFromString("#ff0000"), Color))