For the life of me I can't get this binding to work.
On the top is my attempt, on the bottom is an example I found online, which works.
<StackPanel Orientation="Horizontal" Height="100" Width="200" Background="White">
<ItemsControl x:Name="shortcutsItems" Width="100" ItemsSource="{Binding}">
Hello world
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="Normal text"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ListView x:Name="shortcutsList" Width="100">
<ListView.View>
<GridView x:Name="gridShortcuts">
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
public partial class ExamShortcuts : UserControl {
public ExamShortcuts() {
InitializeComponent();
ObservableCollection<Shortcut> shortcuts = new ObservableCollection<Shortcut>();
shortcuts = new ObservableCollection<Shortcut>();
shortcutsList.ItemsSource = shortcuts;
shortcutsItems.ItemsSource = shortcuts; // ERROR!!!
//shortcuts.Add(new Shortcut() { Name = "Shortcut Exam 1" });
//shortcuts.Add(new Shortcut() { Name = "Shortcut Exam 2" });
}
}
public class Shortcut {
public string Name { get; set; }
}
When I run, it says "Items collection must be empty before using ItemsSource" for shortcutsItems.ItemsSource = shortcuts;
Note that is IS EMPTY!!!!! And of course it doesn't work when the Add
lines are uncommented, either.
I've tried using DataContext = shortcuts
and ItemsControl ItemsSource="{Binding]"
with the same result.
Also, all examples I've seen have the ItemSource
assignment in the code after the items are added anyway!
The shortcutsList
works fine.