Search code examples
c#xamluwpxbind

UWP XAML x:Bind inherited interfaces are not recognized


When using x:Bind in a UWP XAML application consider the following:

interface IBaseInterface
{
    string A { get; set; }
}
interface ISubInterface : IBaseInterface
{
    string B { get; set; }
}
class ImplementationClass : ISubInterface
{
    public string A { get; set; }
    public string B { get; set; }
}

In the Page class we have the following:

public partial class MainPage : Page
{
    public ISubInterface TheObject = new ImplementationClass { A = "1", B = "2" };
    //All the rest goes here
}

In the MainPage XAML we have the following snippet:

<TextBlock Text={x:Bind Path=TheObject.A}></TextBlock>

Which causes the following compiler error: XamlCompiler error WMC1110: Invalid binding path 'A' : Property 'A' can't be found on type 'ISubInterface'

The following does work however:

<TextBlock Text={x:Bind Path=TheObject.B}></TextBlock>

Does anybody know if it is a known limitation of the UWP XAML platform that inherited interface-properties are not recognized by the compiler? Or should this be considered a bug? Are there any known workarounds?

Help is greatly appreciated. Thanks in advance!


Solution

  • Yes, after doing some test and reseach, it seems that inherited interface-properties are not recognized by the compiler when using the X:Bind.

    As a workaround we can use the traditional Binding instead of the X:Bind as following:

    In the .xaml:

    <Grid Name="MyRootGrid">
             <TextBlock Text="{Binding A}"></TextBlock>
    </Grid>
    

    In the xaml.cs:

    MyGrid.DataContext = TheObject;