0
votes

I've got a simple WPF grid as an example, with each row having the same height (*). Each of three rows has a single textbox inside. I would like to know if it's possible, in XAML, to specify that when the textbox gets focus, the relevant row should increase to 2* (thus increasing the size for the textbox).

Thanks...

-Ben

 <Window x:Class="WpfApplication2.MainWindow"                       
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="MainWindow" Height="350" Width="525">

   <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="*" />
         <RowDefinition Height="*" />
         <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <TextBox Background="AliceBlue" Grid.Row="0" />
      <TextBox Background="AliceBlue" Grid.Row="1" />
      <TextBox Background="AliceBlue" Grid.Row="2" />
   </Grid>
 </Window>
2

2 Answers

0
votes

Are you just trying to make the font size bigger when any textblock gets focus? If yes then here's one way to do it

<Window.Resources>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="FontSize" Value="20"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBox Background="AliceBlue" Grid.Row="0" />
        <TextBox Background="AliceBlue" Grid.Row="1" />
        <TextBox Background="AliceBlue" Grid.Row="2" />
    </Grid>
</Grid>
0
votes

You can set the in the Style value for Grid.Row using Trigger like this:

<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Grid.Row" Value="2" />
        </Trigger>
    </Style.Triggers>
</Style>

But to place all the elements in Grid necessary to set a value for Grid.Row locally. In this case, given the Dependency Property Value Precedence we have in the Style values that will be ignored.

I recommend you just set the Height for the TextBox, like so:

<Window.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Height" Value="100" />
        <Setter Property="Background" Value="AliceBlue" />

        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Height" Value="200" />
                <Setter Property="Background" Value="Beige" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <TextBox Name="TextBox1" Grid.Row="0" /> 
    <TextBox Name="TextBox2" Grid.Row="1" />
    <TextBox Name="TextBox3" Grid.Row="2" /> 
</Grid>

Or hide/show TextBox via EventTriggers depending on the focus. To do this you just for each TextBox to create a EventTrigger with GotFocus and LostFocus events. For more information, please see this:

MSDN: EventTrigger