code_junkie

Setting Focus on a Control within a ControlTemplate in WPF

Nov 11th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. <Style TargetType="{x:Type controls:FieldTextBox}">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="{x:Type controls:FieldTextBox}">
  5. <Border BorderThickness="0" Margin="5">
  6. <StackPanel ToolTip="{Binding Path=Field.HintText, RelativeSource={RelativeSource TemplatedParent}}">
  7. <TextBlock Text="{Binding Path=Field.FieldLabel, RelativeSource={RelativeSource TemplatedParent}}"
  8. HorizontalAlignment="Left"
  9. />
  10. <TextBox Width="{Binding Path=Field.DisplayWidth, RelativeSource={RelativeSource TemplatedParent}}"
  11. HorizontalAlignment="Left"
  12. Text="{Binding Path=Field.Data.CurrentValue, RelativeSource={RelativeSource TemplatedParent}}"
  13. IsEnabled="{Binding Path=Field.IsEnabled, RelativeSource={RelativeSource TemplatedParent}}"
  14. ContextMenu="{Binding Source={StaticResource FieldContextMenu}}" >
  15. <TextBox.Background>
  16. <SolidColorBrush Color="{Binding Path=Field.CurrentBackgroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
  17. </TextBox.Background>
  18. </TextBox>
  19. </StackPanel>
  20. </Border>
  21. </ControlTemplate>
  22. </Setter.Value>
  23. </Setter>
  24. <Setter Property="Focusable" Value="True" />
  25. <Setter Property="IsTabStop" Value="False" />
  26. </Style>
  27.  
  28. Control control = (Control)this.Template.FindName("FocusableControl", this);
  29. if (control != null)
  30. {
  31. control.Focus();
  32. }
Add Comment
Please, Sign In to add comment