1

I'm trying to programmatically generate combo box items. I'm very new to WPF and don't understand where I'm making a mistake.

This is my List<string>:

public class StatusList : List<string>
{
    public StatusList()
    {
        this.Add("aaa");
        this.Add("bbb");
        this.Add("ccc");
        this.Add("ddd");
    }
}

And I'm trying to show these items in

<DataTemplate>
    <ComboBox Height="22" ItemsSource="{StaticResource StatusList}" SelectedItem="{Binding Status}" />
</DataTemplate>

But ItemsSource="{StaticResource StatusList}" is not recognized

4
  • use an observablecollection Commented Feb 28, 2018 at 13:02
  • also, instead of ItemsSource="{StaticResource StatusList}" try ItemsSource="{Binding StatusList}" Commented Feb 28, 2018 at 13:03
  • @Sudsy1002 Thanks for tips. ItemsSource="{Binding StatusList}" do not work, I'll look at obervableCollection Commented Feb 28, 2018 at 13:06
  • StatusList is a class not a property, try Rahul's answer Commented Feb 28, 2018 at 13:13

2 Answers 2

4

You need to first set the DataContext property of your MainWindow, which will provide a default source object for any Bindings where a source is not explictly set (by setting either Source, RelativeSource or ElementName).

The object held by the DataContext is typically called a view model.

Your view model should have a public property Statuses which returns a List<string>

Then in XAML you can declare ItemsSource="{Binding Statuses}"

Statuses may also be declared as ObservableCollection<string> in case you want the UI to updated when elements are added or removed.

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

2 Comments

Can you elaborate a little more on ViewModel? How can I set it?
Basically ViewModel we refer more in context of MVVM pattern. Let it be some class say EmployeeViewModel. You can set datacontext for your view in your code behind .cs file constructor as this.DataContext=new EmployeeViewModel() 'EmployeeViewModel' is expected to expose public property say Statuses which can return List<string>
1

{StaticResource StatusList} - StatusList here is not type name, it is a resource key.

for {StaticResource} to work it should be defined somethere:

<Window.Resources>
   <local:StatusList x:Key="StatusList"/>
</Window.Resources>

local is an alias for namespace where StatusList is declared. local should be declared in xaml using xmlns

2 Comments

stupid question, but Where can I use <Window.Resources> without getting an error? I tried to add it to <Grid.Resources> before asking a question, but it is not recognized.
Even if I add <Window.Resources> after <Grid> tag, the warning appears that I'm missing assembly reference.

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.