Pergunta

Não consigo ter a ideia mais simples de um ItemControl trabalhar. Eu só quero povoar meu ItemsControl com um monte de SomeItem.

Este é o código-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"));
        }
    }
}

Este é o 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>

O que estou fazendo errado?

Foi útil?

Solução

Certifique -se de que seu datacontext esteja definido como RelativeRce Self em algum lugar do seu XAML. Se você pudesse postar mais do seu xaml, pode ser útil.

<ItemsControl... DataContext="{Binding RelativeSource={RelativeSource Self}}" >

Outras dicas

O itens Source faz referência a uma propriedade chamada MyParticipants, enquanto o código parece ser participante.

Verifique a exibição de saída de depuração e você verá as mensagens de erro.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top