First place the following extension method in static class (either place the class in the same namespace as the rest of your code or in namespace included with a using
statement in your code file):-
public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)
{
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(root, i);
yield return child;
foreach (var descendent in Descendents(child))
yield return descendent;
}
}
With this extension method available you can dig out the ScrollViewer inside the text box which is responsible for the scroll bar and test its ComputedVerticalScrollBarVisibility
.
if (textbox1.Descendents().OfType<ScrollViewer>()
.FirstOfDefault().ComputedVerticalScrollBarVisibility == Visibility.Visible)