0

I have a ListBox I want to fill with data from two TextBoxesby clicking a Button. I think the problem comes from the differents textblock i have in my listbox. Here is what i want in image : TheUI The MainWindow.xaml of my listbox :

<ListBox x:Name="listBox" 
              ItemsSource="{Binding Issues}"  Grid.Column="1" HorizontalAlignment="Left" Height="366" VerticalAlignment="Top" Width="453" Margin="0,0,-1,0">
        <StackPanel Margin="3">
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Issue:"
              DockPanel.Dock="Left"
              Margin="5,0,10,0"/>
                <TextBlock Text="  " />
                <TextBlock Text="{Binding Issue}"  Foreground="Green" FontWeight="Bold" />
            </DockPanel>
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Comment:" Foreground ="DarkOrange"
              DockPanel.Dock="Left"
              Margin="5,0,5,0"/>
                <TextBlock Text="{Binding Comment}" />

            </DockPanel>
        </StackPanel>
    </ListBox>

My MainWindow.xaml.cs

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    public sealed class ViewModel
    {
        public ObservableCollection<Issue> Issues { get; private set; }

        public ViewModel()
        {
            Issues = new ObservableCollection<Issue>();
        }
    }

    private void addIssue_Click(object sender, RoutedEventArgs e)
    {
        var vm = new ViewModel();
        vm.Issues.Add(new Issue { Name = "Jon Skeet", Comment = "lolilol" });
        DataContext = vm;
        InitializeComponent();
    }
}

My Issue.cs :

public sealed class Issue
{

    public string Name { get; set; }
    public string Comment { get; set; }
}

I follow this tutorial but i don't want to implement a Database : Tuto I also try to use this stackoverflow question The error i have is 'System.InvalidOperationException' The Items collection must be empty to use ItemsSource But not sure this is the heart of the problem.

Community
  • 1
  • 1
Babuh
  • 15
  • 1
  • 10
  • Why are you setting your datacontext on each button click? – Pikoh Apr 25 '16 at 09:07
  • Because i want to add a pair of Issue/Comment each time i press the button – Babuh Apr 25 '16 at 09:14
  • You don't need to do that. ObservableCollection takes care of updating the UI each time a new Issue is added – Pikoh Apr 25 '16 at 09:15
  • Even if i want the UI to update only when i press the button ? – Babuh Apr 25 '16 at 09:19
  • Yes, because is only when you press the button when you are adding a new object to your ObservableCollection. Anyway, try changing in your Listbox the textblock bindings from ` – Pikoh Apr 25 '16 at 09:22

2 Answers2

0

You don't need to update Context and InitializeComponent every time, atleast to your case.

public partial class MainWindow : Window
{
    ViewModel vm = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
        DataContext = vm;
    }


    public sealed class ViewModel
    {
        public ObservableCollection<Issue> Issues { get; private set; }

        public ViewModel()
        {
            Issues = new ObservableCollection<Issue>();
        }
    }

    private void addIssue_Click(object sender, RoutedEventArgs e)
    {       
        vm.Issues.Add(new Issue { Name = "Jon Skeet", Comment = "lolilol" });       
    }
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • i don't have the error anymore but this does not add anything to the `Listbox` :/ EDIT : Well my bad i have the same error and this does not open the window anymore – Babuh Apr 25 '16 at 09:13
0

Remove whatever you have inserted between <ListBox> and </ListBox>, as it is treated as part of Items collection.

Instead shift that content between <ListBox.ItemTemplate>...</ListBox.ItemTemplate>.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38