DataGridUserCommand Binding Converters and Events

1 posts, 0 answers
  1. Julien
    Julien avatar
    6 posts
    Member since:
    Mar 2016

    Posted 30 Jan 2019 Link to this post


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


    01.Public Class CustomGenerateColumnCommand
    02.    Implements ICommand
    04.    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
    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="""" xmlns:x=""""  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
    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



Back to Top