我有一个带有许多子项的标准 WPF 树视图项。以编程方式创建树时,我将选定的事件处理程序添加到父项和子项。我发现的是,当子项选定事件被触发并被处理后,父事件被触发。这个问题是我根据所选项目填充数据网格。因此,每次选择子项后,数据网格都会重置为父项选择。
请有人解释这个项目是正常的还是我做错了什么以及如何修复它。
如果您需要更多信息,请告诉我。
连接事件处理程序:
//parent
TreeViewItem tvi = new TreeViewItem();
tvi.Header = str;
tvi.Selected += CoreSupplierSelected
//child
TreeViewItem tvi = new TreeViewItem();
tvi.Header = str;
tvi.Tag = resinSystems.Values[i];
tvi.Selected += CoreResinSystemSelected;
处理程序:
private void CoreSupplierSelected(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
MaterialSelectionData thicknessData = editInterface.GetCoreThicknessData(new List<object>() { item.Tag }, null);
List<string> columnNames = thicknessData.DisplayFieldTitles;
columnNames.Insert(0,"");
DataTable dt = GUICommon.DatableConverter.ToDataTable(thicknessData.DisplayData, columnNames);
dtgCores.ItemsSource = dt.AsDataView();
dtgCores.Columns[0].Visibility = System.Windows.Visibility.Collapsed;
}
private void CoreResinSystemSelected(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
GX3MaterialSelectionData thicknessData = editInterface.GetCoreThicknessData(new List<object>() { ((TreeViewItem)item.Parent).Tag }, new List<object>() { item.Tag });
List<string> columnNames = thicknessData.DisplayFieldTitles;
columnNames.Insert(0, "");
DataTable dt = GUICommon.DatableConverter.ToDataTable(thicknessData.DisplayData, columnNames);
dtgCores.ItemsSource = dt.AsDataView();
e.Handled = true;
dtgCores.Columns[0].Visibility = System.Windows.Visibility.Collapsed;
}