如何将 AutoCompleteBox 中的选定项目添加到 ListBox 或 Datagrid?
我正在尝试构建一个场景,在该场景中我可以从项目集合中选择任何项目,并能够通过单击“添加”按钮将此文本字符串添加到列表框或数据网格中。我还需要能够通过单击“删除”按钮从列表框或数据网格中删除该项目。我开始了这个,但无法使其发挥作用。我想知道问题是什么。任何想法都受到高度赞赏。谢谢你!
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;
}
}
I am trying to build the scenario where I can pick any item from a collection of items and being able to add this text string to a listbox or datagrid by clicking ‘Add’ button. I also need to be able to remove the item from listbox or datagrid by clicking 'Remove' button. I started this but have problem to make it work. I am wondering what the problem is. Any ideas are highly appreciated. Thank you!
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>
Code Behind:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者来说,如果您使用 ObservableCollection<>对于 myDataList 而不是 List<>您只需添加和删除项目,控件就会自动更新。
其次,在迭代项目时尽量不要删除它们。首先将它们放在单独的列表中。
最后,您在哪里创建 myDataList ? :)
For starters, if you use an ObservableCollection<> for myDataList instead of List<> you can just add and remove items and the control will auto-update.
Second, try not to do remove items while iterating over them. Put them in a seperate List first.
And finally, where are you even CREATING myDataList ? :)
您无需为自动完成框指定名称并以这种方式检查其选定的项目,只需绑定到其 selectedItem 属性即可。然后在“AddButton_Click”中,您可以简单地添加您绑定到的 selectedItem。
Instead of giving the autocomplete box a name and checking its selected item that way, you can just bind to its selectedItem property. Then in your "AddButton_Click" you can simply add the selectedItem you bind to.