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> <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; }

What are the uses of BindingModes?

The most common binding scenarios involve the data getting updated from the source to the target. But, in some cases, the source has to be updated when changes are made by the users in the target. Such cases can be defined using the ‘BindingModes’ of the Binding markup extension class. ‘BindingModes’ is an enumeration property and it takes the following values. 1. OneWay – The target is updated whenever the source is updated. 2. TwoWay – A change to either the target or source updates the other. 3. OneWayToSource – It is an opposite of OneWay. The source is updated whenever the target value changes. 4. OneTime – It is similar to OneWay except that the changes to the source are also not reflected in the target. The target contains the value of the source when Binding was initiated.

What is DataContext ? How is it used ?

Every FrameworkElement can be associated with a DataContext which will be used as the default data source during binding, if no other data source is specified in the binding code. Also, the children of this FrameworkElement auotmatically inherit this setting. This helps having to repeatedly specify this data source as the source of a binding, thereby letting you simplify the binding code. The following code snippet is an example of implicit datasource using DataContext. [XAML] <StackPanel x:Name=’Class’ DataContext='{StaticResource student}’> <Label x:Name=’studcount’ Content='{Binding Path=Count}’/> <ListBox x:Name=’Studname’ DisplayMemberPath=’Name’ ItemsSource='{Binding}’/> </StackPanel>

How to access elements that are embedded inside the ControlTemplate ?

When you declare elements inside the ControlTemplate, use the ‘Name’ of each control for identifying it from the outside. We can then use the ‘FindName’ function for finding the resource. An example shown below is for accessing the TextBlock of a button. [XAML] <Button Background=’LightGoldenrodYellow’> <Button.Style> <Style TargetType=’Button’> <!–Set to true to not get any properties from the themes.–> <Setter Property=’OverridesDefaultStyle’ Value=’True’/> <Setter Property=’Template’> <Setter.Value> <ControlTemplate TargetType=’Button’> <Grid> <Ellipse Name=’innerEllipse’ Fill='{TemplateBinding Background}’/> <ContentPresenter HorizontalAlignment=’Center’ VerticalAlignment=’Center’/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> </Button> [C#] // From inside the custom Button type: Ellipse ellipse = this.Template.FindName(‘innerEllipse’, this) as Ellipse;

What are the requirements for a property to be animated?

WPF enables users to apply animations to objects by applying animations on properties. For a property to have animation capabilities, it should have the following requirements. The property should be a dependency property. It must belong to a class that inherits from the DependencyObject class and implements the IAnimatable interface. There must be a compatible Animation type available for the property. For e.g. the Double type property can be animated using the DoubleAnimation. Similarly, the property to be animated should contain the corresponding animation type.