WPF: Más simple ItemsControl no mostrará ningún artículo
-
22-09-2019 - |
Pregunta
No puedo obtener la idea más simple de un ItemControl
de trabajo. Sólo quiero llenar mi ItemsControl
con un montón de SomeItem
.
Este es el código subyacente:
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 es el 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>
¿Qué estoy haciendo mal?
Solución
Asegúrese de que su DataContext se establece en Auto RelativeSource algún lugar de su xaml. Si puede publicar más de su xaml que podría ser útil.
<ItemsControl... DataContext="{Binding RelativeSource={RelativeSource Self}}" >
Otros consejos
Las referencias ItemsSource una propiedad llamada MyParticipants, mientras que el código que parece ser participantes.
Comprobar el punto de vista de salida de depuración, y debería ver mensajes de error.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow