如何将选定的项目从AutoCompleteBox添加到ListBox或Datagrid?

时间:2011-03-06 23:44:50

标签: wpf silverlight-4.0 wpf-controls wpftoolkit

我正在尝试构建一个场景,我可以从项目集合中选择任何项目,并且可以通过单击“添加”按钮将此文本字符串添加到列表框或数据网格中。我还需要能够通过单击“删除”按钮从列表框或数据网格中删除该项目。我开始这个但是有问题让它工作。我想知道问题是什么。任何想法都受到高度赞赏。谢谢!

XAML:

<UserControl 
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
x:Class="AutoCompleteBoxSimpleTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
mc:Ignorable="d" >

<StackPanel x:Name="LayoutRoot" Background="White" Width="150">
    <TextBlock Text="{Binding ElementName=MyAutoCompleteBox, Path=SelectedItem, TargetNullValue='No item selected', StringFormat='Selected Item: {0}'}" />      
    <sdk:AutoCompleteBox x:Name="MyAutoCompleteBox" IsTextCompletionEnabled="True" ItemsSource="{Binding Items}" />
    <Button x:Name="AddButton" Click="AddButton_Click" Content="AddButton" />
    <Button x:Name="RemoveButton" Click="RemoveButton_Click" Content="RemoveButton" />
    <ListBox x:Name="ListBox" BorderThickness="0" SelectionMode="Multiple" />

    <sdk:DataGrid x:Name="dgEditPackageProperties_ADEntities">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn x:Name="dgtcEditPackageProperties_Icon"/>
            <sdk:DataGridTextColumn x:Name="dgtcEditPackageProperties_Entities" Header="AD Entities with Access" /> 
        </sdk:DataGrid.Columns> 
    </sdk:DataGrid>
</StackPanel>

代码背后:

public partial class MainPage : UserControl
{
    private IList<string> myDataList = null;
    string currentItemText;
    public IList<string> Items
    {
        get;
        private set;
    }

    public MainPage()
    {
        InitializeComponent();

        Items = new List<string>();
        Items.Add("One");
        Items.Add("Two");
        Items.Add("Three");
        Items.Add("Four");

        DataContext = this;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        if (MyAutoCompleteBox.SelectedItem != null)
        {

            foreach (var item in MyAutoCompleteBox.SelectedItem)
            {
                ListBox.Items.Add(item);
                myDataList.Remove(item);
            }
            ApplyDataBinding();
        }
    }

    private void RemoveButton_Click(object sender, RoutedEventArgs e)
    {
        if (ListBox.SelectedItems != null)
        {
            int count = ListBox.SelectedItems.Count - 1;
            for (int i = count; i >= 0; i--)
            {
                //myDataList.Add(ListBox.SelectedItems[i]);
                ListBox.Items.Remove(ListBox.SelectedItems[i]);
            }
            ApplyDataBinding();
        }
    }

    private void ApplyDataBinding()
    {
        MyAutoCompleteBox.ItemsSource = null;
        MyAutoCompleteBox.ItemsSource = myDataList;
    }

}

2 个答案:

答案 0 :(得分:1)

对于初学者,如果您使用ObservableCollection&lt;&gt; for myDataList而不是List&lt;&gt;您只需添加和删除项目,控件就会自动更新。

其次,尽量不要在迭代它们时删除项目。首先将它们放在一个单独的列表中。

最后,你甚至在哪里创建myDataList? :)

答案 1 :(得分:0)

您可以只绑定到其selectedItem属性,而不是为自动填充框指定名称并以该方式检查其所选项目。然后在“AddButton_Click”中,您只需添加绑定到的selectedItem。