Results 1 to 2 of 2

Thread: Change color of Rectangle in a cell in DataGrid WPF VB.NET

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    23

    Change color of Rectangle in a cell in DataGrid WPF VB.NET

    Hi people.
    I have a DataGrid in WPF.
    There are two columns in it.
    All datas at the first column are a rectangle (I mean as Shape).
    There is a button in the window, that when user hits the button, it fills the Rectangle of row 35, to Red.

    But I don't know why VS gives me exception that it is nothing!

    Would you please kindly help me on this?




    What I have tried:
    Code:
    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:RectangleREF"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <DataGrid x:Name="DGEditing"
                      HorizontalAlignment="Stretch" 
                      VerticalAlignment="Stretch"
                      HorizontalContentAlignment="Stretch"
                      VerticalContentAlignment="Stretch"
                      AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="" IsReadOnly="True" Width="2*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Rectangle RadiusX="5" RadiusY="3.5" Fill="Black">
                                </Rectangle>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Character" IsReadOnly="False" Width="22*" Binding="{Binding Path=CharacterName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Button" HorizontalAlignment="Left" Margin="650,149,0,0" VerticalAlignment="Top" Height="91" Width="91" Click="Button_Click"/>
        </Grid>
    </Window>

    Code:
    Class MainWindow
        Public Class TemporaryItemsClass
            Public Property Rct As Boolean
            Public Property CharacterName As String
        End Class
        Public TemporaryItems As New System.Collections.ObjectModel.ObservableCollection(Of TemporaryItemsClass)
        Sub New()
            InitializeComponent()
            For I As Integer = 1 To 1000
                TemporaryItems.Add(New TemporaryItemsClass With {
                    .Rct = False,
                    .CharacterName = "Number" & I
                    })
            Next I
            DGEditing.ItemsSource = TemporaryItems
        End Sub
        Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
            Dim row = TryCast(DGEditing.ItemContainerGenerator.ContainerFromIndex(35), DataGridRow)
            Dim cell As DataGridCell = GetCell(DGEditing, row, 0)
            Dim rectangle = TryCast(cell.Content, Rectangle)
            rectangle.Fill = Brushes.Red
        End Sub
        Private Function GetCell(dg As DataGrid, rowContainer As DataGridRow, column As Integer) As DataGridCell
            Dim returner
            If rowContainer IsNot Nothing Then
                Dim presenter = VisualTreeHelper.GetParent(rowContainer)
                Dim cell As DataGridCell = TryCast(dg.Columns(column).GetCellContent(presenter), DataGridCell)
                If cell IsNot Nothing Then returner = cell
            End If
            Return returner
        End Function
    End Class
    Last edited by Sherlock_Holmes; May 14th, 2024 at 01:55 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width