Generic.xaml - x:Bind with Converter binding

guess_me_if_u_can 126 Reputation points
2023-12-20T18:24:50.85+00:00

I have tried to create my own templated control that should work on x:Bind. By reading the offical document, I have created a code-behind Generic.xaml.cs file and referred it in App.xaml. When I use a converter inside my control, then it throws null pointer exception from the auto generated file of Generic.xaml.

Please refer the following repo for the full code.

https://github.com/Naveen61097/xBindGeneric

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
747 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,151 Reputation points Microsoft Vendor
    2023-12-21T04:50:32.46+00:00

    Use Binding.

    <CalendarDatePicker
        DateFormat="{x:Bind Format, Mode=OneWay}"
        Date="{Binding StartDate, Converter={StaticResource DateToStringConverter}, Mode=TwoWay}"
        />
    

    User's image


  2. Xiaopo Yang - MSFT 12,151 Reputation points Microsoft Vendor
    2023-12-22T05:12:49.3366667+00:00
    • Is there no other way to use {x:bind} with Converters?

    It's not mentioned in document. See x:Bind in ControlTemplate.

    But as {x:Bind} markup extension said,

    The {x:Bind} markup extension—new for Windows 10—is an alternative to {Binding}. {x:Bind} runs in less time and less memory than {Binding} and supports better debugging.

    Finally, I found that the exception is raised because private class Generic_obj2_Bindings.localResources is null. no like the generated code for Pages which also uses x:Bind.

    Screenshot 2023-12-22 125457_LI

    Screenshot 2023-12-22 125721_LI

    Not sure if It‘s a bug in the generated code. My solution is to modify the file Generic.g.cs.

    Screenshot 2023-12-22 131025_LI

    Screenshot 2023-12-22 131217