Search code examples
wpftextboxsetfocus

How to set focus in a textbox when user click an expander?


I have an expander...and a textbox. The textbox is hidden by default. when user click on the expander, I am displaying the text box. This is working fine.

What I need is, when user click on the expander...I need to set the focus in the textbox.

Please help me to do this...I tried with following code...but it seems "IsFocused" is readonly property.

Any help would be appreciated !

<StackPanel>
  <DockPanel>
    <TextBlock DockPanel.Dock="Left"  Text="ID"/>
      <Expander x:Name="ID" DockPanel.Dock="Right" IsExpanded="False" ExpandDirection="Down">                                                    
      </Expander>
  </DockPanel>
  <TextBox Text="{Binding Path=SearchCCCId.Value,UpdateSourceTrigger=PropertyChanged}" 
     Visibility="{Binding ElementName=ID,Path=IsExpanded,Converter={x:Static local:Converters.BoolToVisibility}}"  Width="70" >
   <TextBox.Style>
     <Style>
        <Style.Triggers>
           <DataTrigger Binding="{Binding ElementName=ID, Path=IsEpanded}" Value="True" >
              <Setter Property="IsFocused" Value="True" />
           </DataTrigger>
         </Style.Triggers>
     </Style>
    </TextBox.Style>
   </TextBox>
</StackPanel>

Solution

  • IsFocused is ReadOnly property. You may want to look at these -

    How to set focus to textbox using MVVM?

    WPF - Set Focus when a button is clicked - No Code Behind