views:

116

answers:

2

I can't get the simplest idea of an ItemControl to work. I just want to populate my ItemsControl with a bunch of SomeItem.

This is the code-behind:

using System.Collections.ObjectModel;
using System.Windows;

namespace Hax
{

    public partial class MainWindow : Window
    {

        public class SomeItem
        {
            public string Text { get; set; }
            public SomeItem(string text)
            {
                Text = text;
            }
        }

        public ObservableCollection<SomeItem> Participants
            { get { return m_Participants; } set { m_Participants = value; } }
        private ObservableCollection<SomeItem> m_Participants
            = new ObservableCollection<SomeItem>();

        public MainWindow()
        {
            InitializeComponent();
            Participants.Add(new SomeItem("Hej!"));
            Participants.Add(new SomeItem("Tjenare"));
        }
    }
}

This is the XAML:

<ItemsControl Name="itemsParticipants"
    ItemsSource="{Binding Path=Participants}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Red" BorderThickness="2">
                <TextBlock Text="{Binding Text}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

What am I doing wrong?

+1  A: 

The ItemsSource references a property called MyParticipants, whereas the code it looks to be Participants.

Check the Debug Output view, and you should see error messages.

Thies
I'm sorry, that was just a mistake when copy-pasting the code, replacing some variable names. I'll update the question.
Deniz Dogan
Cool. I still recommend looking at the Debug Output view, as you should see error message there that may lead to the answer.
Thies
+1  A: 

Make sure your datacontext is set to RelativeSource Self somewhere in your xaml. If you could post more of your xaml it might be helpful.

<ItemsControl... DataContext="{Binding RelativeSource={RelativeSource Self}}" >
Guy Starbuck
I added `DataContext="{Binding RelativeSource={RelativeSource Self}}"` to the `Window` element which solved the problem. Is this the standard way?
Deniz Dogan
Yes, it's probably most standard to just add it at the Window level. You can technically add it anywhere in the tree, as long as it is above the element you are binding.
Guy Starbuck
@Guy: Your answer is correct but your comment contains an error. If you add a `DataContext` binding anywhere in the tree except at the Window level, `RelativeSource Self` won't work for the code shown: you must use `RelativeSource FindAncestor` instead.
Ray Burns
@Ray Burns, you are correct, thanks for the update -- if you are binding the DataContext to RelativeSource Self and want it to bind to the CodeBehind for the Window from a nested element, you need to use RelativeSource FindAncestor as Ray says.
Guy Starbuck