This is a migrated thread and some comments may be shown as answers.

DataGridUserCommand Binding Converters and Events

0 Answers 69 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 31 Jan 2019, 04:27 AM

Hello,

In my datagrid, I use a custom DataTemplate for one of my (autogenerated) columns:

 

01.Public Class CustomGenerateColumnCommand
02.    Implements ICommand
03. 
04.    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
05. 
06.    Public Sub Execute(parameter As Object) Implements ICommand.Execute
07.        Dim context As GenerateColumnContext = CType(parameter, GenerateColumnContext)
08.        If context.PropertyName = "myColumnName" Then
09.            Dim c As New DataGridTemplateColumn
10.            c.CellContentTemplate = CType(XamlReader.Load("" &
11.                "<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  xmlns:common=""using:MyNS.Converters"">" &
12.                    "<Grid Background=""{Binding StatusColor}"">" &
13.                        "<Grid.ColumnDefinitions>" &
14.                            "<ColumnDefinition Width=""70""/>" &
15.                            "<ColumnDefinition Width=""220"" /> " &
16.                        "</Grid.ColumnDefinitions>" &
17.                            "<TextBlock Text=""{Binding Title}"" Style=""{StaticResource SubtitleTextBlockStyle}"" TextTrimming=""CharacterEllipsis"" Height=""20""  FontSize=""16""/>" &
18.                    "</Grid>" &
19.                "</DataTemplate>"), DataTemplate)
20.            c.Name = "myColumnName"
21.            context.Result = c
22.        End If
23.    End Sub
24. 
25.    Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
26.        'exclude those properties
27.        Dim context As GenerateColumnContext = CType(parameter, GenerateColumnContext)
28.        Return context.PropertyName <> "StatusColor" And context.PropertyName <> "Title"
29.    End Function

The above works perfectly. Both properties StatusColor and Title are used in one custom formatted cell.

My questions:

  • I have been unable to use a binding converter to modify the appearance of the data to be displayed

For example like this:

<Grid Background="{Binding StatusColor, Converter={StaticResource myColorConverter}}">

Having myColorConverter well defined and accessible in myNS.Converters. I have tried to add the static resource in the DataTemplate, or in the grid resources or even in the top page xaml resources, but each time I get "resource could not be found". 

How can I use a Binding Converter in this situation ?

 

  • I have been unable to attach an event to any of the template's Element. The goal is to navigate to a new frame of the application when the user taps the generated cell above.

For example like this:

<TextBlock Text="{Binding Title}"  Tapped="myTxt_Tapped" x:Name="myTxt" />

My code behind would of course contain an event handler like this:

Private Sub myTxt_Tapped(sender As Object, e As TappedRoutedEventArgs)
'navigate to nextPage
End Sub

But that never gets fired. And I would expect it to fail anyway because my grid has multiple rows and thus I get multiple times the same TextBlock name ; meaning I need to attach an event handler, but how, and at which moment in the page lifecycle ?

I have also tried to use the CellTap DataGridCommand, but when I use 

Public Overrides Sub Execute(parameter As Object)
    Dim context = CType(parameter, DataGridCellInfo)
    If context.Column.Name = "myColumnName" Then
        Dim dob As myObject= CType(context.Item.myColumnName, myObject)
        CType(Window.Current.Content, Frame).Navigate(GetType(nextPage), dob)
    End If
End Sub

the last line throws a Memory Violation Exception.

How can I register an event ? How can I navigate to a new frame ?

 

Thanks for any help

Julien

 

No answers yet. Maybe you can help?

Tags
DataGrid
Asked by
Julien
Top achievements
Rank 1
Share this question
or