绑定到 Activity 的 XAML ListBox 项目

发布于 2024-12-27 09:29:45 字数 1216 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2025-01-03 09:29:45

好的,您的代码中存在一些可能导致问题的错误。
在吸气剂中,我认为你应该有这个。

if (_selectedItems == null)
{
   _selectedItems = new ObservableCollection<string>();
}
return _selectedItems;

在您的版本中,_selectedItems 永远不会被初始化。

在 Xaml 代码中,当您设置 ItemSource 时,您编写了 Seleceteditems 而不是 SelectedItems 此错误在编译时不会导致错误,但您的 listBox 没有其itemSource 设置为正确的元素。

然后,您没有在

ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}

中指定源,这意味着默认情况下源是对象的 DataContext,并且 DataContext 应该使用具有名为 ModelItem 的公共属性的对象进行初始化,该对象又具有名为 Selecteditems 的公共属性。
希望它有效。

这是一个小例子。
在我的 xaml 文件中

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListBox Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="LstSelectedPosts" VerticalAlignment="Top" Width="294" 
             ItemsSource="{Binding Path=SelectedItems, Mode=TwoWay}"/>
    <Button Content="Add Item" HorizontalAlignment="Stretch" Click="Button_Click" Margin="321,110,68,170"/>
    <Button Content="Remove Item" HorizontalAlignment="Stretch" Click="DelButton_Click" Margin="321,147,68,129"/>
</Grid>

在我的 xaml.cs 文件

public partial class MainWindow : Window
{

    private CDataContext _myCDataContext;

    public MainWindow()
    {
        InitializeComponent();
        _myCDataContext = new CDataContext();
        DataContext = _myCDataContext;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _myCDataContext.Add();
    }

    private void DelButton_Click(object sender, RoutedEventArgs e)
    {
       _myCDataContext.Remove(LstSelectedPosts.SelectedItem.ToString());
    }


}

和我的 CDataContext 类中

 class CDataContext
{
    private int _count = 0;
    private ObservableCollection<string> _selectedItems;
    public ObservableCollection<string> SelectedItems
    {
        get
        {
            if (_selectedItems == null)
            {
                _selectedItems = new ObservableCollection<string>();

            }
            return _selectedItems;
        }
        set
        {
            _selectedItems = value;
        }
    }

    public void Remove(string s)
    {
        SelectedItems.Remove(s);
    }

    public void Add()
    {
        SelectedItems.Add(_count.ToString());
        _count++;
    }
}

Ok there are some errors in your code that could cause the problem.
In the getter, I think you should have this.

if (_selectedItems == null)
{
   _selectedItems = new ObservableCollection<string>();
}
return _selectedItems;

In your version, _selectedItems never get initialized.

In the Xaml code, when you set the ItemSource, you wrote Seleceteditems instead of SelectedItems 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 named Selecteditems.
Hope it works.

Here is a small example.
in my xaml file

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListBox Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="LstSelectedPosts" VerticalAlignment="Top" Width="294" 
             ItemsSource="{Binding Path=SelectedItems, Mode=TwoWay}"/>
    <Button Content="Add Item" HorizontalAlignment="Stretch" Click="Button_Click" Margin="321,110,68,170"/>
    <Button Content="Remove Item" HorizontalAlignment="Stretch" Click="DelButton_Click" Margin="321,147,68,129"/>
</Grid>

in my xaml.cs file

public partial class MainWindow : Window
{

    private CDataContext _myCDataContext;

    public MainWindow()
    {
        InitializeComponent();
        _myCDataContext = new CDataContext();
        DataContext = _myCDataContext;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _myCDataContext.Add();
    }

    private void DelButton_Click(object sender, RoutedEventArgs e)
    {
       _myCDataContext.Remove(LstSelectedPosts.SelectedItem.ToString());
    }


}

and my CDataContext class

 class CDataContext
{
    private int _count = 0;
    private ObservableCollection<string> _selectedItems;
    public ObservableCollection<string> SelectedItems
    {
        get
        {
            if (_selectedItems == null)
            {
                _selectedItems = new ObservableCollection<string>();

            }
            return _selectedItems;
        }
        set
        {
            _selectedItems = value;
        }
    }

    public void Remove(string s)
    {
        SelectedItems.Remove(s);
    }

    public void Add()
    {
        SelectedItems.Add(_count.ToString());
        _count++;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文