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 8483251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:02:46+00:00 2026-06-10T20:02:46+00:00

I’ve got some maintenance work to do on some WPF controls, which is something

  • 0

I’ve got some maintenance work to do on some WPF controls, which is something I’m not really familiar with, and I am struggling with some fundamentals in WPF.

I have the following code, which I understand is referred to as “code-behind”:

Class MainWindow
    Private _myStrings As New List(Of String)({"one", "two", "three", "four", "five"})
    Public Property myStrings As List(Of String)
        Get
            Return _myStrings
        End Get
        Set(value As List(Of String))
            _myStrings = value
        End Set
    End Property
End Class

I then have this WPF stuff, which defines a really ugly ComboBox.

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Margin="0,10,0,0"
                  x:Name="cboItem" 
                  TabIndex="1"/>
    </Grid>
</Window>

Question: All I want to know is how do I correctly create references in the XAML to display the myStrings List in the cboItem ComboBox? Once I know that, I can get around to understanding databinding concepts in detail, but right now, I need someone to explain really basic things for me like “How do I tell XAML where to look for data?”

  • 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-10T20:02:48+00:00Added an answer on June 10, 2026 at 8:02 pm

    ComboBox has a property called ItemsSource that can be set to either a static list of strings, or more commonly, can be bound to some list of objects.

    WPF objects look for their bindings in their DataContext. This is a property of every WPF framework element, and will “cascade” down, so setting the DataContext of the Window means that every control on that window will also inherit the same DataContext. However, they do not have to use the same context. Every control can have its own context set simply by setting its DataContext property.

    You have defined your list of strings in the window itself (in the code behind). This is not really usual. A more common method used in WPF is to define a ViewModel class that contains all the data your view needs, and then set that as the DataContext. This is what the MVVM pattern is all about.

    However, going with your example, there is nothing stopping you from setting your Window’s DataContext to the Window itself:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}">
        <Grid>
            <ComboBox Margin="0,10,0,0"
                      x:Name="cboItem" 
                      TabIndex="1"
                      ItemsSource="{Binding myStrings}"/>
        </Grid>
    </Window>
    

    The DataContext line tells WPF which object to look at for its bindings, and the ItemsSource line tells the combo which property to use on the context for its list of strings.

    EDIT: To set the DataContext on the combo you could do:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ComboBox Margin="0,10,0,0"
                      x:Name="cboItem" 
                      TabIndex="1"
                      DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}"
                      ItemsSource="{Binding myStrings}"/>
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.