Filling a ListBox from two TextBoxes with DataBinding

时间:2016-04-25 09:02:18

标签: c# wpf xaml data-binding listbox

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.

2 个答案:

答案 0 :(得分: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" });       
    }
}

答案 1 :(得分: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>.