1

Having a simple XAML user control, I'd like to set the DataContext to the code behind (xaml.cs) file.

I'd like to set DataContext and Itemssource in XAML, so I can populate the combobox with property ListOfCars

XAML

<UserControl x:Class="Sample.Controls.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="85" d:DesignWidth="200">
    <Grid Height="85" Width="200" Background="{StaticResource MainContentBackgroundBrush}">
        <StackPanel Orientation="Vertical">                
            <ComboBox Height="23.338" x:Name="CarList" />                
        </StackPanel>    
    </Grid>
</UserControl>

Code behind

public List<Cars> ListOfCars
{
  get { return _store.ListCars(); }
}

In other words, instead of doing this in codebehind, how may I set the binding in XAML

public MyControl()
{
  InitializeComponent();
  _store = new Store();
  CarList.ItemsSource = _store.ListCars();
  CarList.DisplayMemberPath = "Name";
}
Kman
  • 4,809
  • 7
  • 38
  • 62

2 Answers2

1

Just bind the ItemsSource.

<ComboBox ItemsSource="{Binding ListOfCars}"/>

And then for the UserControl:

<MyControl DataContext="{Binding *viewModel*}"/>

You have to bind the DataContext where your UserControl is used rather than in the definition, because in the definition you don't know to what to bind. The Combobox automatically is in the context of the control so you can just bind to the DataContext without any additional work.

Example of binding to a resource:

<Application.Resources>
  ...
  <viewmodels:ViewModelLocator x:Key="ViewModelLocator"/>
  ...
</Application.Resources>


<MyControl DataContext="{Binding Source={StaticResource ViewModelLocator}}"/>

This creates an instance of the ViewModelLocator and then binds the DataContext of the control to that resource.

N_A
  • 19,799
  • 4
  • 52
  • 98
  • Don't quite get the {Binding viewModel}. It's not working, but how may viewModel be binded to the code behind, without hooking it up in someway. Don't have a specific viewmodel for this xaml – Kman Sep 07 '12 at 13:41
  • You have to define your view model somewhere. This can be the `DataContext` in which you use your control (then bind like this `{Binding}`) or it can be a static resource (then bind like this `{Binding Source={StaticResource *resource name*}}`, but you have to define what you're binding to somewhere. – N_A Sep 07 '12 at 13:43
  • Read through this for some basics on binding to a view model: http://msdn.microsoft.com/en-us/library/hh821028.aspx – N_A Sep 07 '12 at 13:46
  • Thanks, .. I understand that I should read up on this as my binding knowledge is very limited at the moment :) – Kman Sep 07 '12 at 13:49
  • But I'm really not using the MVVM pattern for this, .. or at least not to my understanding. Just like to do the binding in xaml instead of in code behind (see question update) – Kman Sep 07 '12 at 13:53
  • That doesn't really matter. The basics of binding are the same. You have to either use the current `DataContext` or use some sort of relative binding. Whether you're using MVVM or not you have to set the `DataContext` in order to bind to it. – N_A Sep 07 '12 at 13:56
  • See my edit for an example of how to bind to a static resource. – N_A Sep 07 '12 at 14:08
1

Do not do that, you will mess up all external bindings to the DataContext. Use UserControl.Name and ElementName bindings instead (or RelativeSource).

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I don't think that's what he was intending to do. It seems more like he was just confused about how to set the binding. – N_A Sep 07 '12 at 13:51