Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8496521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:52:40+00:00 2026-06-10T23:52:40+00:00

Having a simple XAML user control, I’d like to set the DataContext to the

  • 0

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";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:52:41+00:00Added an answer on June 10, 2026 at 11:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having some problems with binding in wpf/xaml. Have this simple file: <Window x:Class=test.Window1
I'm having a simple test design problem, which I would like to solve once
I am having a simple code where i want to know when does onRestoreInstanceState
Having the following very simple xaml: <DocumentViewer Name=dv> <FixedDocument Name=fd Loaded=fd_loaded> <FixedDocument.Resources> <Style x:Key=TestStyle>
Having a simple Akka HTTP server: package org.package.some import akka.actor.{IOManager, IO, Actor} import java.net.InetSocketAddress
I really need help on this one. I am having a simple login form
hey guys having this really simple problem but cant seem to figure out have
I'm having a very simple problem. I'm new to WordPress and I'm trying to
I am having a very simple requirement.I need the border on left and right
I've created this simple demo because I'm having some trouble making auto-sizing work in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.