firelex
firelex

Reputation: 135

Stop TextBox resizing when user types

I'm implementing a TimePicker. As basis I took this one. It is very nice and has almost all I need.

The problem is: it's TextBoxes are resized every time user changes the text.

I need these TextBoxes to have a constant width, sufficient for two digits regarding current font/font size.
It is surely possible to set a fix number for width or MinWidth, but in case of font/font size changes I'll have to calculate it again.


UPDATE

Here is my XAML:
<UserControl x:Class="TimePickerCtrl.TimePicker"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:TimePickerCtrl"
         mc:Ignorable="d" 
         Height="Auto" Width="Auto" x:Name="UserControl" 
         d:DesignHeight="100" d:DesignWidth="300">
<Grid x:Name="LayoutRoot" Width="Auto" Height="Auto">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="0.25*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>

    <!-- Hours -->
    <Grid Grid.Column="0" x:Name="grdH" >
        <TextBox x:Name="txtH" Text="{Binding Path=Hours, ElementName=UserControl, Mode=Default, StringFormat={}{0:D2}}" 
                 TextAlignment="Center" VerticalAlignment="Center" FontFamily="Goudy Stout" FontSize="14" />
    </Grid>

    <!-- Separator -->
    <Grid Grid.Column="1">
        <TextBox Text=":" IsReadOnly="True" VerticalAlignment="Center" TextAlignment="Center" BorderThickness="0" Focusable="False" Background="{x:Null}"/>
    </Grid>

    <!-- Minutes -->
    <Grid Grid.Column="2" x:Name="grdM" >
        <TextBox x:Name="txtM" Text="{Binding Path=Minutes, ElementName=UserControl, Mode=Default, StringFormat={}{0:D2}}" 
                  TextAlignment="Center" VerticalAlignment="Center" FontFamily="Goudy Stout" FontSize="14" />
    </Grid>
  </Grid>
</UserControl>

I've left out all event handling code checking input and it's length for the sake of simplicity. "Hours" and "Minutes" are simple dependency properties.

Thanks in advance.

Upvotes: -1

Views: 52

Answers (0)

Related Questions