我有TabItem
个DataGrid
,DataTemplate
只有一个DataGrid
。
现在我将访问<Window.Resources>
<DataTemplate x:Key="ContentTabItem">
<DataGrid Grid.Row="1" Name="_uiDataGrid"/>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl Name="_uiTabControl" SelectionChanged="_uiTabControl_SelectionChanged">
<TabItem Name="_uiTabItembla1" Header="bla1" ContentTemplate="{StaticResource ResourceKey=ContentTabItem}"/>
<TabItem Name="_uiTabItembla2" Header="bla2" ContentTemplate="{StaticResource ResourceKey=ContentTabItem}"/>
</TabControl>
</Grid>
,因为我想更改内容......
_uiDataGrid
我无法使用名称ggplot2
答案 0 :(得分:0)
尝试这样的方法从内容演示者内容模板中获取数据网格:
var contentPresenter = FindVisualChild<ContentPresenter>(_uiTabControl);
var dt = contentPresenter.ContentTemplate;
var datagrid = DataTemplateName.FindName("_uiDataGrid", contentPresenter)
as DataGrid;
要查找视觉儿童,请参阅this stackoverflow post.
中提及的FindVisualChildren()
和FindVisualChild()
功能
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
public static childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
foreach (childItem child in FindVisualChildren<childItem>(obj))
{
return child;
}
return null;
}