-2

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.

Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
  • _"Note that is IS EMPTY!!!!!"_ -- you can put as many exclamation marks as you like, that doesn't change the fact that the collection is _not_ empty, and that you would never see that exception if it were. Debugging the code would've revealed to yourself what was going on. – Peter Duniho Apr 15 '20 at 21:20
  • Debugging with the debugger? It would be nice for you to have explained in what way it was not empty. I did know the "Hello World" was there but I obviously didn't understand that it "counted" as the source. I was going off of `shortcuts` being empty. – Jonathan Tuzman Apr 15 '20 at 21:27

1 Answers1

0

Remove "Hello World" and the ItemSource binding parts prior to setting the ItemsSource in your codebehind. You are trying to set it dynamically, but have already set it in your xaml which is why it think's the source is not empty. I bet that's causing your issue.

Change

<ItemsControl x:Name="shortcutsItems" Width="100" ItemsSource="{Binding}">
     Hello world

to

<ItemsControl x:Name="shortcutsItems" Width="100">

Tronald
  • 1,520
  • 1
  • 13
  • 31
  • Oh my god I hate myself. I definitely tried this several times before inserting the Hello World but something in between must have changed that would have worked if it weren't there. Thank you. – Jonathan Tuzman Apr 15 '20 at 21:19
  • Such is the life of programming haha, we all go there daily! Glad it worked. – Tronald Apr 15 '20 at 21:21