在我的TabControl
OnSelectionChanged
活动中,我从之前选择的标签页中删除了装饰,并在新选择的标签页中添加了装饰。
private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var tabControl = (TabControl)sender;
var previousTabItem = (TabItem)e.RemovedItems[0];
var newTabItem = (TabItem)e.AddedItems[0];
RemoveTabItemsControlsAdorners(tabControl, previousTabItem);
var tabControlChildrenSelected = TabControlHelper.GetTabControlAnalysisElements(UIHelper.FindVisualChildrenOfMultipleTypes(UIHelper.AnalysisControlTypes, tabControl.FindChildContentPresenter(newTabItem)));
var details = (InputControlEventDetails)(tabControl).Tag;
tabControlChildrenSelected = tabControlChildrenSelected.Concat(new[] { newTabItem });
AddAdorners(tabControlChildrenSelected, details.InputControlEvents, details.NumberOfLoggedViews);
}
这一行给了我新选择的TabItem的元素(它不是空的):
var tabControlChildrenSelected = TabControlHelper.GetTabControlAnalysisElements(UIHelper.FindVisualChildrenOfMultipleTypes(UIHelper.AnalysisControlTypes, tabControl.FindChildContentPresenter(newTabItem)));
在我的TabControlHelper中,我对可见元素进行了过滤。但是新选择的TabItem的所有元素都还不可见。所以GetTabControlAnalysisElements()
返回一个空集合。所以我尝试通过反射检查可见性,但methodInfo
始终为空。
public static IEnumerable<FrameworkElement> GetTabControlAnalysisElements(IEnumerable<FrameworkElement> elements)
{
var visibleElements = elements.Where(element => WouldBeVisible(element));
return visibleElements;
}
public static bool WouldBeVisible(FrameworkElement element)
{
// Returns true if the control would be visible if container is visible
var methodInfo = element.GetType().GetMethod("GetState", BindingFlags.Instance | BindingFlags.NonPublic);
if (methodInfo == null) return element.IsVisible;
return (bool)(methodInfo.Invoke(element, new object[] { 2 }));
}
如何获取新选择的TabItem的可见项?