c#代码修改xaml中listbox的datatemplate中的控件
public static void SearchVisualTree(DependencyObject targetElement, int width,string Node)
{
var count = VisualTreeHelper.GetChildrenCount(targetElement);
if (count == 0)
{
return;
}
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targetElement, i);
if (child is TextBlock)
{
TextBlock targetItem = (TextBlock)child;
if (targetItem.Name == Node)
targetItem.Width = width;
}
else
{
SearchVisualTree(child, width,Node);
}
}
}
{
var count = VisualTreeHelper.GetChildrenCount(targetElement);
if (count == 0)
{
return;
}
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targetElement, i);
if (child is TextBlock)
{
TextBlock targetItem = (TextBlock)child;
if (targetItem.Name == Node)
targetItem.Width = width;
}
else
{
SearchVisualTree(child, width,Node);
}
}
}
用法:DBHelper.SearchVisualTree(listboxname,300,"textBox1");