我在DataGrid中有一个ListBox。我使用了一个ObservableCollection来填充ListBox项。如何遍历每行中的ListBox项?请注意,将有多个包含ListBox的列。
提前致谢。
public ObservableCollection<string> PotIncHazCollection
{
get { return _PotIncHazCollection; }
set
{
_PotIncHazCollection = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("PotIncHazCollection"));
}
}
我的UserControl编码为......
<DataGrid Name="dataGridJobSteps"
ItemsSource="{Binding}"
InitializingNewItem="dataGridJobSteps_InitializingNewItem"
AutoGenerateColumns="False"
CanUserSortColumns="True"
SelectionMode="Single"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
VerticalScrollBarVisibility="Visible"
Height="Auto" Width="Auto" FontWeight="Normal">
<DataGrid.Columns>
<DataGridTextColumn Header="JobSafetyAnalysisStepID" Binding="{Binding Path=JobSafetyAnalysisStepID}" Visibility="Hidden" />
<DataGridTextColumn Header="Step #" Binding="{Binding Path=StepNumber}" />
<DataGridTemplateColumn Header="Description of Job Steps" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=StepDescription}" TextWrapping="Wrap" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=StepDescription}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Height="50" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Potential Incidents or Hazards" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding PotIncHazCollection}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--<TextBox Text="{Binding Path=selectedPotentialHazardPotentialHazard}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Height="50" />-->
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Controls and Checks Required" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding ControlsChecksCollection}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--<TextBox Text="{Binding Path=selectedPotentialHazardPotentialHazard}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Height="50" />-->
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Actioned By" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding ActionedByCollection}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--<TextBox Text="{Binding Path=selectedPotentialHazardPotentialHazard}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Height="50" />-->
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
填充DataGrid Rows和ListBox项的My Code Behind ...
private void getJSAStepDetail()
{
try
{
DataTable jobSteps;
String query = "select JobSafetyAnalysisStepPotentialHazardID \"JobSafetyAnalysisStepPotentialHazardID\""
+ ", StepDescription \"StepDescription\""
+ ", StepNumber \"StepNumber\""
+ ", RefJobSafetyAnalysisStepID \"RefJobSafetyAnalysisStepID\""
+ " from JobSafetyAnalysisPotentialHazard_view"
+ " where RefPermitID = '" + thisRefPermitID + "' Group by RefJobSafetyAnalysisStepID";
jobSteps = db.GetDataTable(query);
List<JobSafetyAnalysisData> jobStepItems = new List<JobSafetyAnalysisData>();
foreach (DataRow r in jobSteps.Rows)
{
if (jobStepItems != null)
{
string refStepId = r["RefJobSafetyAnalysisStepID"].ToString();
String potHazQuery = "select PotentialHazard from JobSafetyAnalysisPotentialHazard_view where RefJobSafetyAnalysisStepID = '" + refStepId + "'";
DataTable refSteps = db.GetDataTable(potHazQuery);
ObservableCollection<string> colRefSteps = new ObservableCollection<string>();
foreach (DataRow refStepsRow in refSteps.Rows)
{
colRefSteps.Add(refStepsRow["PotentialHazard"].ToString());
}
jobStepItems.Add(new JobSafetyAnalysisData()
{
JobSafetyAnalysisStepID = r["RefJobSafetyAnalysisStepID"].ToString(),
StepNumber = r["StepNumber"].ToString(),
StepDescription = r["StepDescription"].ToString(),
PotIncHazCollection = colRefSteps
});
}
}
dataGridJobSteps.ItemsSource = jobStepItems;
}
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
确定上面按照下面的代码进行了排序,但它只是迭代了第一列ListBox(很棒),但还有两个其他列数据项需要从...中获取值...
private void button1_Click(object sender, RoutedEventArgs e)
{
// Update Job Step Grid ListBox
for (int i = 0; i < dataGridJobSteps.Items.Count; i++)
{
DependencyObject obj = dataGridJobSteps.ItemContainerGenerator.ContainerFromIndex(i);
ListBox _listBox = FindVisualChild<ListBox>(obj);
var selectedItems = _listBox.Items;
foreach (var selectedItem in selectedItems)
{
MessageBox.Show(selectedItem.ToString());
}
}
}
public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
// Search immediate children
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}