:: Home

  login:         
  passwords:  

WPF FAQs

WPF General Questions
Concepts Element Tree
Concepts Dependency Property
Concepts Attached Property
Concepts Routed Events
Concepts Resources.
Concepts Animation
Concepts Freezable Object
Concepts Input and Commands.
Concepts Layouts
XAML Inline Styles and Templates
XAML XML Namespaces
XAML Code Behind
XAML Custom Class
XAML Type Converters
Content Model ItemControl
Content Model HeaderedItemsControl
Content Model HeaderedContentControl
Content Model Content Control
Content Model Decorator Content Model
Content Model Panel Content Model
Documents Serialization and Storage
Documents Annotations
Documents Flow Content
Documents Printing
Graphics and Multimedia Rendering Graphics
Graphics and Multimedia Animation and Timing
Graphics and Multimedia 2D Graphics
Graphics and Multimedia 3D Graphics
Graphics and Multimedia Visual Layer
Control Customization Adorners
Control Customization Stylable controls
Control Customization ControlTemplates
Data Data Binding
Data DragandDrop
Data Serialization
Globalization and Localization Attributes
Globalization and Localization Comments
Globalization and Localization Globalization Struc
Application and Deployment ClickOnce
Application and Deployment Frame
Application and Deployment Page
Application and Deployment Navigation
Application and Deployment Setup
Application and Deployment Window
Interoperability Message Loops Between
Interoperability Win32 in WPF
Interoperability WPF in Win32
Interoperability Windows Forms and WPF
Security Trusted Security
Security Partial Trust Security
Tools Microsoft Expression Blend
Tools ZAM3D
Tools XAMLPAD
Controls ToolTip
Controls TextBlock
Controls TabControl
Controls ProgressBar
Controls PrintDialog
Controls Popup
Controls TextBox
Controls Canvas
Controls ComboBox
Controls ListBox
Controls StatusBar
Controls ToolBar
Controls Context Menu
Controls Expander
Controls DocumentViewer
Controls FlowDocumentReader
Controls GroupBox
Controls GridSplitter
Controls Image
Controls Menu
Controls ScrollBar
Controls Slider
Controls RichTextBox
Controls Border
Controls Buttons

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

Winforms Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Controls TextBlock Interview Questions & FAQs

Questions and answers for Controls TextBlock.

56. Controls TextBlock

    56.1 How can I make a TextBlock editable when it is clicked and the value is committed when ESC or ENTER is pressed?

56.1 How can I make a TextBlock editable when it is clicked and the value is committed when ESC or ENTER is pressed?

 

[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>

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;

}

bottom user control
::  Home :: Services ::  Prices ::  Request Quote
Copyright 2007, Megasolutions Ltd