working not left .net wpf layout textbox stretch

.net - not - wpf padding left



Los cuadros de texto de WPF son demasiado grandes (2)

Supongo que el contenedor de su TextBox está causando que sea demasiado grande.

Prueba el siguiente XAML en Kaxaml :

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBox Text="Sample text" FontSize="2" /> </Grid> </Page>

Esto se representa como un cuadro de texto muy pequeño en el centro de la página. Si elimina VerticalAlignment="Center" y HorizontalAlignment="Center" del contenedor, entonces el cuadro de texto es muy grande.

Las alineaciones horizontales y verticales predeterminadas para Grid y TextBox son Stretch , lo que básicamente significa que al elemento no le importa y tomará lo que se le da. Entonces, el motor de diseño pregunta al TextBox qué tan grande debería ser y no obtiene una respuesta, por lo que el diseño le pide a la Grid . Tampoco le importa a la Grid , así que finalmente le pide a la Page / Window , que tiene un tamaño fijo, y luego este tamaño se propaga por el árbol visual (teniendo en cuenta los márgenes y el relleno a lo largo del camino). El resultado neto es que TextBox llena toda el área.

Para probar este punto, mueva los atributos de alineación de la Grid al propio TextBox . No puede ver una diferencia visualmente, aunque si establece un color de fondo para la cuadrícula lo haría.

<Grid Background="Red"> <TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Text="Sample text" FontSize="2" /> </Grid>

Si desea colocar el borde del cuadro de texto junto al texto, también puede configurar Padding="0" en el cuadro de texto.

Consulte este artículo para obtener más información sobre el sistema de diseño de WPF.

Tengo una página WPF con algunos cuadros de texto de entrada de datos y se ven mucho más grandes que las necesidades de fuentes. ¿Qué determina la altura del cuadro de texto? ¿Hay alguna manera de aplastarlos?

El cuadro de texto se hace más grande y más pequeño de acuerdo con el tamaño de fuente que muestra (por lo que no quiero establecer la propiedad de altura directamente si puedo evitarlo).

Aquí hay un ejemplo de lo que quiero decir ...

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <Style x:Key="LabelStyle" TargetType="Label"> <Setter Property="HorizontalAlignment" Value="Right"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> </Style> <Style x:Key="TextBoxStyle" TargetType="TextBox"> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> </Style> </Page.Resources> <StackPanel> <WrapPanel> <Label Style="{StaticResource LabelStyle}" Content="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/> <TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/> </WrapPanel> <WrapPanel> <Label Style="{StaticResource LabelStyle}" Content="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/> <TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/> </WrapPanel> </StackPanel> </Page>

Si miras el tamaño, verás que la etiqueta es un poco más grande que el cuadro de texto. Cambiar la alineación vertical en el cuadro de texto a la parte superior hace que sea del mismo tamaño. Como medida provisional, establecí un margen en la etiqueta a -2.


Aquí hay un ejemplo de lo que quiero decir ...

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <Style x:Key="LabelStyle" TargetType="Label"> <Setter Property="HorizontalAlignment" Value="Right"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> </Style> <Style x:Key="TextBoxStyle" TargetType="TextBox"> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> </Style> </Page.Resources> <StackPanel> <WrapPanel> <Label Style="{StaticResource LabelStyle}" Content="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/> <TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/> </WrapPanel> <WrapPanel> <Label Style="{StaticResource LabelStyle}" Content="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/> <TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/> </WrapPanel> </StackPanel> </Page>

Si miras el tamaño, verás que la etiqueta es un poco más grande que el cuadro de texto. Cambiar la alineación vertical en el cuadro de texto a la parte superior hace que sea del mismo tamaño. Como medida provisional, establecí un margen en la etiqueta a -2.