绑定到 Activity 的 XAML ListBox 项目
我正在使用 Microsoft 活动库设计器;由于某些原因,我需要使用 ListBox 来显示其中的一些信息。但是我的 ItemsSource 绑定有问题。我的 Activity 侧属性如下所示:
private ObservableCollection<string> _selectedItems;
public ObservableCollection<string> SelectedItems
{
get
{
if (_selectedItems == null)
{
ObservableCollection<string> items = new ObservableCollection<string>();
return items;
}
return _selectedItems;
}
set
{
_selectedItems = value;
}
}
我的 XAML 侧代码如下:
....
<Button Content="Add Item" HorizontalAlignment="Stretch" Grid.Column="0"
Click="Button_Click" Margin="5, 0, 5, 5"/>
<Button Content="Remove Item" HorizontalAlignment="Stretch" Grid.Column="1"
Click="DelButton_Click" Margin="5, 0, 5, 5"/>
....
<ListBox x:Name="LstSelectedPosts" MinHeight="20" ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}"/>
....
现在当我尝试在添加项目和删除项目按钮中向此列表框添加/删除项目时,单击事件,调试器向我显示一个错误,告诉我无法修改ListBox绑定源。 那么我怎样才能改变这个列表框的项目呢?
I'm using Microsoft Activity Library Designer; For some reasons I need to use ListBox to show some information in it.But I have a problem with it's ItemsSource binding.My Activity side property is like this:
private ObservableCollection<string> _selectedItems;
public ObservableCollection<string> SelectedItems
{
get
{
if (_selectedItems == null)
{
ObservableCollection<string> items = new ObservableCollection<string>();
return items;
}
return _selectedItems;
}
set
{
_selectedItems = value;
}
}
And my XAML side code is like this:
....
<Button Content="Add Item" HorizontalAlignment="Stretch" Grid.Column="0"
Click="Button_Click" Margin="5, 0, 5, 5"/>
<Button Content="Remove Item" HorizontalAlignment="Stretch" Grid.Column="1"
Click="DelButton_Click" Margin="5, 0, 5, 5"/>
....
<ListBox x:Name="LstSelectedPosts" MinHeight="20" ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}"/>
....
Now when I try to Add/Remove an item to/from this ListBox in Add Item and Remove Item buttons click event, debugger shows me an error that tells I can't modify the ListBox binding source.
So how can I change this Listbox's Items?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,您的代码中存在一些可能导致问题的错误。
在吸气剂中,我认为你应该有这个。
在您的版本中,_selectedItems 永远不会被初始化。
在 Xaml 代码中,当您设置 ItemSource 时,您编写了
Seleceteditems
而不是SelectedItems
此错误在编译时不会导致错误,但您的 listBox 没有其itemSource 设置为正确的元素。然后,您没有在
ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}
中指定源,这意味着默认情况下源是对象的 DataContext,并且 DataContext 应该使用具有名为
ModelItem
的公共属性的对象进行初始化,该对象又具有名为Selecteditems
的公共属性。希望它有效。
这是一个小例子。
在我的 xaml 文件中
在我的 xaml.cs 文件
和我的 CDataContext 类中
Ok there are some errors in your code that could cause the problem.
In the getter, I think you should have this.
In your version, _selectedItems never get initialized.
In the Xaml code, when you set the ItemSource, you wrote
Seleceteditems
instead ofSelectedItems
this error doesn't cause an error when you compile but your listBox doesn't have its itemSource setted to the correct element.And then, you didn't specify the source in
ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}
that means the source is by default, the DataContext of your object and that DataContext should be initialized with an object that has a public property named
ModelItem
which has in turn a public property namedSelecteditems
.Hope it works.
Here is a small example.
in my xaml file
in my xaml.cs file
and my CDataContext class