【问题标题】:How to dynamically show ListBoxes in two synchronized boxes如何在两个同步框中动态显示列表框
【发布时间】:2018-10-16 10:49:21
【问题描述】:

我目前正在将数据从一个列表框传输到另一个列表框。 使用 WPF 我有:

            <Grid>
                <ListBox Margin="10,29,194,301" Name="LeftListBox"/>
                <ListBox Margin="0,29,16,301" Name="RightListBox" HorizontalAlignment="Right" Width="173" />
                <Button Name="AddButton" Height="23" Margin="34,135,227,0" VerticalAlignment="Top"
                Click="AddButton_Click">Add &gt;&gt;</Button>
                <Button Name="RemoveButton" Margin="227,135,34,264" 
                Click="RemoveButton_Click">&lt;&lt; Remove</Button>
            </Grid>

对于我的 C# 代码,我创建了两个方法,通过使用字符串数组和右侧的字符串来加载左侧框的元素。 现在我的问题是我希望将左框的元素放在右框列表的最后一个元素之后的右框中。所以当我点击添加时,它应该执行这个方法:

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        // Find the right item and it's value and index
        currentItemText = LeftListBox.SelectedValue.ToString();
        currentItemIndex = LeftListBox.SelectedIndex;

        ObservableCollection<string> oList;
        oList = new System.Collections.ObjectModel.ObservableCollection<string>(toRemoveList);
        RightListBox.DataContext = oList;

        Binding binding = new Binding();
        RightListBox.SetBinding(ListBox.ItemsSourceProperty, binding);

        (RightListBox.ItemsSource as ObservableCollection<string>).Add(currentItemText);
        if (toAddList != null)
        {
            toAddList.RemoveAt(currentItemIndex);
        }

        // Refresh data binding
                   ApplyDataBinding();
    }

但问题是,当我从左侧框中选择一个项目,然后单击添加时,它会将新项目添加到右侧框中,但是当我添加第二个项目时,它会替换我添加的最后一个项目第一步。

之后,第二个问题是,如何实现 RemoveButton_Click ?和之前的方法一样吗?

【问题讨论】:

  • 您在添加时定义了一个新的Binding 对象。
  • 所以最好在构造函数中定义它吧?
  • 可以,否则会被覆盖

标签: c# xml wpf


【解决方案1】:

您无需为此编写这么多代码。按照下面给出的步骤,以获得更强大和可维护的方法。

  1. 创建两个Observablecollection,分别对应左右ListBox
  2. Observablecollection绑定到ListBox
  3. 点击Add按钮,从分配给左侧列表框的可观察集合中移除item,并添加到绑定到右侧网格的可观察集合中。

您无需显式更新绑定。 Observable 集合会自动通知集合的变化。

在代码中-

public ObservableCollection<ApplicationFormats> Formats { get; set; }

Xaml -

<ListBox Name="LeftListBox" ItemsSource="{Binding Formats}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【讨论】:

  • 在这种情况下是否可以将字符串列表绑定到列表框?我的意思是,我真的需要创建一个 ObservableCollection 吗?
  • 您已经在新的System.Collections.ObjectModel.ObservableCollection&lt;string&gt;(toRemoveList) 中使用Observable collection。它应该可以工作并且具有可维护的代码。
  • 我刚刚添加了您向我展示的行,但列表框中没有出现任何内容。我选择的投标名称是ObservableCollection 的名称,但我仍然看不到错误在哪里
  • 我刚刚尝试了您刚刚在帖子中编辑的内容,但仍然无法找出为什么它只显示 ObservableCollection 中元素的悬停而不是字符串。..
  • 发现它需要是 ` ToAddList = new System.Collections.ObjectModel.ObservableCollection(_toAddList); LeftListBox.ItemsSource = ToAddList;` 在构造函数中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2017-12-27
相关资源
最近更新 更多