[英]Filling a ListBox from two TextBoxes with DataBinding
I have a ListBox
I want to fill with data from two TextBoxes
by clicking a Button. 我有一个
ListBox
我想通过单击一个按钮来填充来自两个TextBoxes
ListBox
数据。 I think the problem comes from the differents textblock
i have in my listbox
. 我认为问题出在我在
listbox
textblock
差异。 Here is what i want in image : TheUI The MainWindow.xaml
of my listbox
: 这是我想要的图像: TheUI我的
listbox
的MainWindow.xaml
:
<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
我的
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
: 我的
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. 我遵循本教程,但我不想实现数据库: Tuto我也尝试使用此stackoverflow问题我遇到的错误是
'System.InvalidOperationException'
The Items collection must be empty to use ItemsSource
但不确定这是核心问题。
You don't need to update Context
and InitializeComponent
every time, atleast to your case. 您无需每次都至少根据情况更新
Context
和InitializeComponent
。
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" });
}
}
Remove whatever you have inserted between <ListBox> and </ListBox>
, as it is treated as part of Items
collection. 删除您在
<ListBox> and </ListBox>
之间插入的所有内容,因为它被视为Items
集合的一部分。
Instead shift that content between <ListBox.ItemTemplate>...</ListBox.ItemTemplate>
. 而是在
<ListBox.ItemTemplate>...</ListBox.ItemTemplate>
之间移动该内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.