Windows 10 Universal Compiled binding (x:Bind) conflict with ViewModel


I want to bind an element in a page to dependency property in code behind with compiled binding and same time bind another element to ViewModel with usual binding. But it gives a runtime error.

Here is my xaml code.

DataContext="{Binding Main, Source={StaticResource Locator}}"
    <DataTemplate x:Key="UserDataTemplate" x:DataType="local:User">
            <TextBlock Text="{x:Bind Name}" />
            <TextBlock Text="{x:Bind Age}" />
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="{Binding Title}"/>
        <ContentPresenter ContentTemplate="{StaticResource UserDataTemplate}" Content="{x:Bind CurrentUser, Mode=OneWay}"/>

Here CurrentUser is dependency property which is initially null and then change in run time. This gives following runtime error.

Incorrect type passed into template. Based on the x:DataType global::XbindingProblem.User was expected.

The problem is it passes the ViewModel into UserDataTemplate instead of CurrentUser dependency property when CurrentUser is null.

Can anyone have a good explanation on this problem?


The question is interesting, what I have done is just remove the datacontext and this is the code behind is similar to yours:

public sealed partial class BlankPage1 : Page
    public User CurrentUser
        get { return (User)GetValue(CurrentUserProperty); }
        set { SetValue(CurrentUserProperty, value); }

    // Using a DependencyProperty as the backing store for CurrentUser.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CurrentUserProperty =
        DependencyProperty.Register("CurrentUser", typeof(User), typeof(BlankPage1), new PropertyMetadata(null));

    public BlankPage1()

        CurrentUser = new User() { Name = "Hello", Age = "20" };

public class User
    public String Name { get; set; }
    public String Age { get; set; }

might be you have the User class in another namespace or have another class in the typeof(...) of the dependency property. Because I tested that and works. The DataContext of the page can be whatever you want it won't affect.

Then I added the datacontext just to test:

Although it breaks the idea of MVVM a bit, you can add a property to your page like this:

public MainViewModel viewModel =

This video can help you solving your question :)
By: admin