WPF 判断一个控件的滚动条是否显示
private bool HasScrollbar(TextBox TextBox)
{
bool HasScrollbar = false;
List<Grid> list = FindVisualChild<Grid>(TextBox);
foreach (var item in list)
{
if (item.Name == "Bg")
{
if (item.IsVisible)
HasScrollbar = true;
}
}
return HasScrollbar;
}
private List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
try
{
List<T> list = new List<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
{
list.Add((T)child);
List<T> childOfChildren = FindVisualChild<T>(child);
if (childOfChildren != null)
{
list.AddRange(childOfChildren);
}
}
else
{
List<T> childOfChildren = FindVisualChild<T>(child);
if (childOfChildren != null)
{
list.AddRange(childOfChildren);
}
}
}
return list;
}
catch (Exception)
{
//MessageBox.Show(ee.Message);
return null;
}
}