私は2つのリストボックスを持っています。それぞれが SQLight クエリからデータを取得します。ユーザーはアイテムを 2 回クリックして選択することができ、LB1 から削除して LB2 に配置する必要があります。ユーザーには LB2 から削除するオプションがあり、選択に満足するまで LB1 に戻す必要があります。LB2 から保存されたデータを使用して、LB1 で見つかったアイテムを除外しています (コードに従って)。ユーザーが選択した項目に満足すると、LB2 データが DB に保存されます。ユーザーはいつでも UI に戻って、必要に応じてアイテムを追加/削除することができます。
2 つの ObservableCollections を使用して LB1 と LB2 を設定しています。テンプレートを使用して、両方の LB のレイアウトを制御しています。IE では、項目が列に 10 個の項目があると言うと、LB 内の新しい列に「こぼれます」。
LB1 を正しく設定し、アイテムを LB2 に移動することもできますが、もう一度元に戻すと...
"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
私は WPF C# の初心者であり、この問題を解決する方法について明確な指示が必要です。(これを解決するために現在4週間取り組んでいます)。ソリューションは、両方の ListBox に適用する必要があります。これについての助けをいただければ幸いです。
添付のコードを確認してコメントしてください...
CS...
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Data;
using System.Data.SQLite;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using PTWS.Class_Lib;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Media;
namespace PTWS.MainPanelControls.FormControls.Permits
{
/// <summary>
/// Interaction logic for ColdWorkDraftUControl.xaml
/// </summary>
public partial class ColdWorkDraftUControl : UserControl
{
PTWDatabase db = new PTWDatabase();
ObservableCollection<CertificateLookup> CertificateLookupList = new ObservableCollection<CertificateLookup>();
ObservableCollection<CertificateSelected> CertificateSelectedList = new ObservableCollection<CertificateSelected>();
public ColdWorkDraftUControl()
{
InitializeComponent();
LoadDataCertificateLookup();
// all works great untill I uncomment the next line
//LoadDataCertificateSelected();
}
private void LoadDataCertificateLookup()
{
try
{
DataTable conditions;
String query = "select lc.Section \"Section\""
+ ", lc.Description \"Description\""
+ ", lc.SortOrder \"SortOrder\" "
+ "from LookupConditions lc "
+ "where not exists (select 1 from SelectedConditions sc where sc.Code = lc.Code and sc.PermitID = 'CWP-12-00001') "
+ "and lc.Section = 'Certificates'";
conditions = db.GetDataTable(query);
foreach (DataRow r in conditions.Rows)
{
CertificateLookupList.Add(new CertificateLookup()
{
Section = r["Section"].ToString(),
Description = r["Description"].ToString(),
SortOrder = r["SortOrder"].ToString()
});
}
listBoxCertificateLookup.ItemsSource = CertificateLookupList;
}
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
void LoadDataCertificateSelected()
{
try
{
DataTable conditions;
String query = "select Section \"Section\""
+ ", Description \"Description\""
+ ", SortOrder \"SortOrder\"";
query += "from SelectedConditions where PermitID = 'CWP-12-00001' and Section = 'Certificates'";
conditions = db.GetDataTable(query);
foreach (DataRow r in conditions.Rows)
{
CertificateSelectedList.Add(new CertificateSelected()
{
selectedSection = r["Section"].ToString(),
selectedDescription = r["Description"].ToString(),
selectedSortOrder = r["SortOrder"].ToString()
});
}
listBoxCertificateSelected.DataContext = CertificateSelectedList;
}
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
private void listBoxCertificateLookup_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
try
{
ListBoxItem myListBoxItem =
(ListBoxItem)(listBoxCertificateLookup.ItemContainerGenerator.ContainerFromItem(listBoxCertificateLookup.Items.CurrentItem));
// listBoxCertificateSelected.DataContext = null;
listBoxCertificateSelected.Items.Add(myListBoxItem);
}
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
private void listBoxCertificateSelected_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
try
{
ListBoxItem myListBoxItem =
(ListBoxItem)(listBoxCertificateSelected.ItemContainerGenerator.ContainerFromItem(listBoxCertificateSelected.Items.CurrentItem));
//listBoxCertificateLookup.DataContext = null;
listBoxCertificateLookup.Items.Add(myListBoxItem);
}
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
}
}
XAML...
<UserControl x:Class="PTWS.MainPanelControls.FormControls.Permits.ColdWorkDraftUControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ListBox Name="listBoxCertificateLookup"
ItemsSource="{Binding CertificateSelectedList}"
MouseDoubleClick="listBoxCertificateLookup_MouseDoubleClick"
IsSynchronizedWithCurrentItem="True"
HorizontalAlignment="Left" Width="300" Height="75" VerticalAlignment="Top" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="23" Orientation="Horizontal">
<TextBlock Text="{Binding Path=Description}" VerticalAlignment="Top" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<ListBox Name="listBoxCertificateSelected"
ItemsSource="{Binding}"
MouseDoubleClick="listBoxCertificateSelected_MouseDoubleClick"
HorizontalAlignment="Left" Width="300" Height="75" VerticalAlignment="Top" Margin="0,153,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="23" Orientation="Horizontal">
<TextBlock Name="textBlock" Text="{Binding Path=selectedDescription}" VerticalAlignment="Top" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</UserControl>
クラス...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PTWS.Class_Lib
{
class CertificateLookup
{
public string Section { get; set; }
public string Description {get; set;}
public string SortOrder { get; set; }
public string selectedSection { get; set; }
public string selectedDescription { get; set; }
public string selectedSortOrder { get; set; }
}
}
修正された CS ファイルの mouseDoubleClick イベント (両方のリストボックスに対して適切に修正されました)...
CertificateLookup myListBoxItem = (CertificateLookup)((ListBox)sender).SelectedItem;
CertificateSelectedList.Add(new CertificateSelected()
{
selectedDescription = myListBoxItem.Description
});
CertificateLookupList.Remove(myListBoxItem);