22

The files I have created and will be referring to in this question are:

TechnicainSelectionView.xaml
TechnicianSelectionView.cs
TechnicianSelectionViewModel.cs
Technician.cs (Code First Entity)

I have the following xaml in my TechnicanSelectionView.xaml

<UserControl xmlns etc... here" 
             d:DesignHeight="48" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <Label Content="Select a Technican to run the test" FontWeight="Bold"></Label>
            <ComboBox ItemsSource="{Binding Technicians, Mode=TwoWay}"></ComboBox>
        </StackPanel>
    </Grid>
</UserControl>

The Technicians property to which the ItemSource is set to bind to states that it Cannot resolve Technicians due to an unknown DataContext.

So if we look to my TechnicianSelectionView.cs code-behind...

public partial class TechnicianSelectionView : UserControl
{
    public TechnicianSelectionViewModel ViewModel { get; private set; }

    public TechnicianSelectionView()
    {
        InitializeComponent();

        Technician.GenerateSeedData();

        ViewModel = new TechnicianSelectionViewModel();
        DataContext = ViewModel;
    }
}

... we see that I am setting the view's DataContext to my TechnicianSelectionViewModel ...

public class TechnicianSelectionViewModel : ViewModelBase
{
    public ObservableCollection<Technician> Technicians { get; set; }

    public TechnicianSelectionViewModel()
    {
        Technicians = new ObservableCollection<Technician>();
    }

    public bool IsLoaded { get; private set; }

    public void LoadTechnicians()
    {
        List<Technician> technicians;

        using (var db = new TestContext())
        {
            var query = from tech in db.Technicians
                        select tech;

            foreach (var technician in query)
            {
                Technicians.Add(technician);
            }
        }

        IsLoaded = true;
    }
}

Techicians is a property on my ViewModel...

So having already set the DataContext for the view, why can't it resolve Technicians on the ViewModel as the DataContext/property it is going to bind to?

EDIT:

As per a concern in a comment below. This is a design time problem and not compile time. I should have indicated this at the start.

Isaiah Nelson
  • 2,450
  • 4
  • 34
  • 53
  • Can you post the full error message? It should state what object it is trying to use for the binding. – Brent Stewart Feb 26 '13 at 23:56
  • @BrentStewart Just an FYI, the full message was what I had typed in the post: `Cannot resolve Technicians due to an unknown DataContext.` – Isaiah Nelson Feb 27 '13 at 00:09
  • Possible duplicate of [How to see design-time data-binding in XAML editor (it works in runtime)?](https://stackoverflow.com/questions/16401885/how-to-see-design-time-data-binding-in-xaml-editor-it-works-in-runtime) – StayOnTarget Apr 09 '19 at 12:04

2 Answers2

46

You need to specify the type of data context in the xaml to get design-time support. Even though you assigned the data context in code-behind, the designer is not going to recognize that.

Try putting the following in your xaml:

d:DataContext="{d:DesignInstance vm:TechnicianSelectionViewModel}"

See this link for more details.

Brandon Baker
  • 1,232
  • 12
  • 16
  • 2
    Is this a design time problem? I didn't see anywhere in the original question where it is mentioned as a design time binding error. – Brent Stewart Feb 26 '13 at 23:58
  • It's not mentioned specifically, but I see this error message frequently during design time, so I'm assuming that's what he's referring to. Furthermore, he states "is going to bind to?", which sounds like design time to me because it sounds like the app isn't running yet. – Brandon Baker Feb 26 '13 at 23:58
  • @BrentStewart It is indeed a design time problem. I failed to mention that. I will update accordingly. – Isaiah Nelson Feb 27 '13 at 00:02
  • @Brandon Thank you for the assistance, Brandon. That definitely worked. And thanks for researching for the article as well. – Isaiah Nelson Feb 27 '13 at 00:06
  • No problem. It's also worth noting that your app would probably run perfectly despite the warning message. I rarely specify my DataContext types and everything comes together nicely at run time. Most of the time I just ignore that warning. – Brandon Baker Feb 27 '13 at 00:09
  • @Brandon I suspected that it would run despite the warning and I guess this was indeed a warning but what I wasn't sure, and I still haven't gotten to that point of finding out if the binding would work. I am still wiring a few other things up first. Thanks again. – Isaiah Nelson Feb 27 '13 at 00:40
  • The link is no longer available. – Ondrej Janacek Sep 22 '16 at 13:11
2

In my Xamarin Forms Xaml file I used the following lines in the header (ContentPage tag) and it worked perfectly as I wanted.

Basically now

  • the intellisense shows the fields in the binding
  • my Resharper is able to rename the binding in the Xaml file if I refactor the name of the property

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:vm="clr-namespace:YourApplicationName.ViewModels;assembly=YourApplicationName"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance {x:Type vm:CurrentPageViewModel}}"
    
Aboo
  • 2,314
  • 1
  • 18
  • 23