[英]How do I get a ScrollViewer inside TabItem In WPF
I need to find a ScrollViewer inside current TabItem and then find a WrapPanel inside that ScrollViewer 我需要在当前TabItem中找到一个ScrollViewer,然后在该ScrollViewer中找到一个WrapPanel
I tried this: 我尝试了这个:
TabItem ti = tabControl.SelectedItem as TabItem;
foreach (ScrollViewer sv in ti.Content)
{
foreach (WrapPanel wp in sv.Content) {}
}
and this 和这个
TabItem ti = tabControl.SelectedItem as TabItem;
foreach (ScrollViewer sv in ti.Children)
{
foreach (WrapPanel wp in sv.Children) {}
}
But doesn't work 但是不起作用
If your tab item contains your scrollviewer directly, you could do the following : 如果您的标签项直接包含您的scrollviewer,则可以执行以下操作:
TabItem ti = tabControl.SelectedItem as TabItem;
ScrollViewer sv = ti?.Content as ScrollViewer;
WrapPanel wp = scrollViewer?.Content as WrapPanel;
Another way of accessing your WrapPanel would be to use a function that returns a child/content of a specific type. 访问WrapPanel的另一种方法是使用返回特定类型的子项/内容的函数。 For instance 例如
public T FindVisualChildOrContentByType<T>(DependencyObject parent)
where T : DependencyObject
{
if(parent == null)
{
return null;
}
if(parent.GetType() == typeof(T))
{
return parent as T;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if(child.GetType() == typeof(T))
{
return child as T;
}
else
{
T result = FindVisualChildOrContentByType<T>(child);
if (result != null)
return result;
}
}
if(parent is ContentControl contentControl)
{
return this.FindVisualChildOrContentByType<T>(contentControl.Content as DependencyObject);
}
return null;
}
Then you would be able to do 那你就可以做
WrapPanel wp = this.FindVisualChildOrContentByType<WrapPanel>(tabItem);
If this is not working, feel free to post your XAML so i can reproduce your exact scenario. 如果这不起作用,请随时发布您的XAML,以便我可以重现您的确切情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.