WPF DataGrid单元格背景色

时间:2018-10-02 21:56:54

标签: c# wpf vb.net xaml

以下代码不错。

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))

0 个答案:

没有答案
相关问题