WPF FAQ - TextBlock

Find answers for the most frequently asked questions
Expand All Collapse All
[XAML]
  
<StackPanel MouseLeftButtonDown='HandleMouseInput' Margin='5'>
            <StackPanel.Resources>
                <!--Base TextBox Style-->
                <Style x:Key='TextBoxBaseStyle' TargetType='{x:Type TextBox}'>
                    <Setter Property='FontSize' Value='20'/>
                    <Setter Property='Margin' Value='5'/>
                </Style>
                <!--Simplified TextBox Style with template & necessary bindings.-->
                <Style
        x:Key='BasicTextBox'
        TargetType='{x:Type TextBox}'
        BasedOn='{StaticResource TextBoxBaseStyle}'>
                    <Setter Property='Template'>
                        <Setter.Value>
                            <ControlTemplate TargetType='{x:Type TextBox}'>
                                <Border
                BorderBrush='{TemplateBinding BorderBrush}'
                BorderThickness='{TemplateBinding BorderThickness}'
                Background='{TemplateBinding Background}'>
                  <TextBlock
                  Margin='3.85,2,3.85,2'
                  Text='{TemplateBinding Text}'
                  TextDecorations='{TemplateBinding TextDecorations}'/>
              </Border>
                   </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property='Focusable' Value='false'/>
                </Style>
            </StackPanel.Resources>
            <TextBox Width='100' Height='30' Style='{StaticResource BasicTextBox}'></TextBox>
    </StackPanel>

The events of the above code are given below.

[C#]

        void HandleMouseInput(object sender, MouseEventArgs args)
        {
            TextBox tb = args.Source as TextBox;
            if (tb == null)
                return;
            if (ActiveTextBox == null)
                Activate(tb);
            else
                Deactivate(ActiveTextBox);
        }

        //Deactivates the active TextBox if the Enter
        // or ESC key is pressed.

        void HandleKeyInput(object sender, KeyEventArgs args)
        {
            TextBox tb = args.Source as TextBox;
            if (tb != null &&
                  (args.Key == Key.Return || args.Key == Key.Escape))
                Deactivate(tb);
        }

        //Deactivate the Textbox the active TextBox.
        void HandleLostFocus(object sender, EventArgs args)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
                Deactivate(tb);
        }

        //Activate the TextBox by applying the base style
        //(thereby removing the basic style.)  Set focus, select
        //all the content & invalidate the visual.
        //Also, dynamically add handlers for losing focus and
        //handling key input.

        void Activate(TextBox tb)
        {
            tb.Style = (Style)tb.FindResource('TextBoxBaseStyle');
            tb.Focus();
            tb.SelectAll();
            tb.InvalidateVisual();
            tb.LostFocus += new RoutedEventHandler(HandleLostFocus);
            tb.KeyDown += new KeyEventHandler(HandleKeyInput);
            ActiveTextBox = tb;
        }

        //Deactivate the TextBox by applying the Basic style.
        //Remove the event handlers.

        void Deactivate(TextBox tb)
        {
            tb.Style = (Style)tb.FindResource('BasicTextBox');
            tb.LostFocus -= new RoutedEventHandler(HandleLostFocus);
            tb.KeyDown -= new KeyEventHandler(HandleKeyInput);
            ActiveTextBox = null;
        }
Permalink

This is very easy to do in XAML, as follows:

[XAML]
        <TextBlock x:Name='myTextBlock' Width='100' Height='40'>
            <Button>Test</Button>
        </TextBlock>

And you can do something similar in C# as follows:

[C#]
            Button btn = new Button();
            btn.Content = 'Test';
            this.myTextBlock.Inlines.Add(btn);
Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.