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";
}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
Read through this for some basics on binding to a view model: msdn.microsoft.com/en-us/library/hh821028.aspx
Thanks, .. I understand that I should read up on this as my binding knowledge is very limited at the moment :)
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)
|
1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.