@Corbett Cappon, Welcome to Microsoft Q&A, you could try to use FormattedText class to get the pixel width of the text entry fields. Here is a code example you could refer to. xaml:
<TextBlock x:Name="text1" Width="100" Height="50" VerticalAlignment="Top" Text="test1"></TextBlock>
c# code:
var formatted = new FormattedText(
text1.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(text1.FontFamily, text1.FontStyle, text1.FontWeight, text1.FontStretch),
text1.FontSize, text1.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
Console.WriteLine(formatted.Width);
we could see the actual pixel width:
Based on my research, it seems that there is no direct way to get all text entry controls. I am afraid that you may could try to use enum to do it, like the following:
private void btntest_Click(object sender, RoutedEventArgs e)
{
//var formatted = new FormattedText(
//text1.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
//new Typeface(text1.FontFamily, text1.FontStyle, text1.FontWeight, text1.FontStretch),
// text1.FontSize, text1.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
foreach (TextBlock tb in FindVisualChildren<TextBlock>(window1))
{
// get textbolock pixel width
double d1 = GetWidth(tb);
Console.WriteLine(d1);
}
foreach (TextBox tb in FindVisualChildren<TextBox>(window1))
{
// get textbox pixel width
double d1 = GetWidth(tb);
Console.WriteLine(d1);
}
foreach (PasswordBox tb in FindVisualChildren<PasswordBox>(window1))
{
// get passwordbox pixel width
double d1 = GetWidth(tb);
Console.WriteLine(d1);
}
}
public double GetWidth(TextBox box)
{
var formatted = new FormattedText(
box.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(box.FontFamily, box.FontStyle, box.FontWeight, box.FontStretch),
box.FontSize, box.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
return formatted.Width;
}
public double GetWidth(TextBlock box)
{
var formatted = new FormattedText(
box.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(box.FontFamily, box.FontStyle, box.FontWeight, box.FontStretch),
box.FontSize, box.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
return formatted.Width;
}
public double GetWidth(PasswordBox box)
{
var formatted = new FormattedText(
box.Password, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(box.FontFamily, box.FontStyle, box.FontWeight, box.FontStretch),
box.FontSize, box.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);
return formatted.Width;
}
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null) yield return (T)Enumerable.Empty<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
if (ithChild == null) continue;
if (ithChild is T t) yield return t;
foreach (T childOfChild in FindVisualChildren<T>(ithChild)) yield return childOfChild;
}
}
Hope my solution could be helpful for you. Best Regards, Jack
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.