Last active
January 2, 2016 12:19
-
-
Save JohanLarsson/8302581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class EditableTextBlock : TextBox | |
| { | |
| public EditableTextBlock() | |
| { | |
| SetTextBlockStyle(); | |
| AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(ToggleTriStateFocus)); | |
| AddHandler(LostFocusEvent, new RoutedEventHandler((sender, args) => SetTextBlockStyle())); | |
| } | |
| private void SetTextBlockStyle() | |
| { | |
| Background = null; | |
| BorderThickness = new Thickness(0); | |
| Padding = new Thickness(-2, 0, -2, 0); | |
| Focusable = false; | |
| IsReadOnly = true; | |
| } | |
| private void SetTextBoxStyle() | |
| { | |
| //Background = null; | |
| BorderThickness = new Thickness(1); | |
| Padding = new Thickness(-3, -2, -3, -2); | |
| IsReadOnly = false; | |
| } | |
| public static readonly DependencyProperty SelectAllTextOnFocusProperty = DependencyProperty.Register("SelectAllTextOnFocus", typeof(bool), typeof(EditableTextBlock), new PropertyMetadata(true)); | |
| [Description("Text: string"), Category("Common Properties")] | |
| public bool SelectAllTextOnFocus | |
| { | |
| get { return (bool)GetValue(SelectAllTextOnFocusProperty); } | |
| set { SetValue(SelectAllTextOnFocusProperty, value); } | |
| } | |
| private void ToggleTriStateFocus(object sender, MouseButtonEventArgs e) | |
| { | |
| if (!SelectAllTextOnFocus) | |
| { | |
| Focusable = true; | |
| Focus(); | |
| SetTextBoxStyle(); | |
| } | |
| if (!Focusable) | |
| { | |
| Focusable = true; | |
| Focus(); | |
| e.Handled = true; | |
| } | |
| else if (IsReadOnly) | |
| { | |
| SetTextBoxStyle(); | |
| SelectAll(); | |
| e.Handled = true; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <StackPanel> | |
| <GroupBox Header="Vanilla TextBlock"> | |
| <StackPanel Orientation="Horizontal"> | |
| <TextBlock Text="Standard TextBlock: "/> | |
| <TextBlock Text="{Binding DummyText}"/> | |
| </StackPanel> | |
| </GroupBox> | |
| <Button>Lose focus here</Button> | |
| <GroupBox Header="Editable TextBlock"> | |
| <StackPanel> | |
| <StackPanel Orientation="Horizontal"> | |
| <TextBlock Text="Editable TextBlock: "/> | |
| <controls:EditableTextBlock x:Name="EditableTextBlock" Text="{Binding DummyText}"/> | |
| </StackPanel> | |
| <CheckBox Content="SelectAllOnFocus" | |
| IsChecked="{Binding ElementName=EditableTextBlock, Path=SelectAllTextOnFocus}"/> | |
| <Grid ap:Grid.Rows="Auto Auto Auto Auto" | |
| ap:Grid.Columns="auto 5 *"> | |
| <TextBlock ap:Grid.Cell="0 0" Text="IsFocused"/> | |
| <TextBlock ap:Grid.Cell="0 2" Text="{Binding ElementName=EditableTextBlock,Path=IsFocused,Mode=OneWay}"/> | |
| <TextBlock ap:Grid.Cell="1 0" Text="Focusable"/> | |
| <TextBlock ap:Grid.Cell="1 2" Text="{Binding ElementName=EditableTextBlock,Path=Focusable,Mode=OneWay}"/> | |
| <TextBlock ap:Grid.Cell="2 0" Text="IsReadOnly"/> | |
| <TextBlock ap:Grid.Cell="2 2" Text="{Binding ElementName=EditableTextBlock,Path=IsReadOnly,Mode=OneWay}"/> | |
| </Grid> | |
| </StackPanel> | |
| </GroupBox> | |
| </StackPanel> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment