在一个行为中(连接到windows.interactivity)我试图找到一个Listview的子项 - 特别是Gridviewcolumn-以便能够调整其宽度,代码是使用MVVM构建的,没有代码隐藏。
问题很简单,就是找不到使用Gridviewcolumn下面的代码,同时还有Maingrid的其他子代,f.e。一个按钮,没问题(参见下面的按钮示例)。
任何错误来自哪些建议?提前谢谢!
// _parent is the main Grid of the Window
Button button = GetTemplateChildByName(_parent); // for the button its working and the value is returned
GridViewColumn gridviewcolumn = GetTemplateChildByNames(_parent);// for the GridViewColumn its not working and no value is returned
....
public Button GetTemplateChildByName(DependencyObject parent)
{
int childnum = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childnum; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is Button)
{
return child as Button;
}
else
{
var s = GetTemplateChildByName(child);
if (s != null)
return s;
}
}
return null;
}
public GridViewColumn GetTemplateChildByNames(DependencyObject parent)
{
int childnum = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childnum; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is GridViewColumn)
{
return child as GridViewColumn;
}
else
{
var s = GetTemplateChildByNames(child);
if (s != null)
return s;
}
}
return null;
}