Volunteer .NET Evangelist

A well oiled machine can’t run efficiently, if you grease it with water.
  首页  :: 联系 :: 订阅 订阅  :: 管理

In-Place Editing In WPF

Posted on 2006-04-27 23:03  Sheva  阅读(3816)  评论(6编辑  收藏  举报
    Several days ago, Dflying Chen just shown us how to implement in-place editing functionality using ASP.NET Atlas framework, and the custom control he writes called InPlaceEditingInput is pretty cool indeed. In this post, I will show you how to achieve the same thing in WPF, and also demonstrate how the WPF makes our lives as programming hobbyists much easier:)
    WPF introduces a new concept called Control template, which means that you can replace the visual tree of any WPF element by defining a new ControlTemplate for the targeting element, and this capability makes writing a custom In-Place Editing WPF control much easier. the following code lists the ControlTemplate I define for the custom textbox control called InfoTextBox:
<Style TargetType="{x:Type itb:InfoTextBox}">
<Setter Property="FontSize" Value="20"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type itb:InfoTextBox}">
<Grid Margin="0">
<Border Name="background"
="{TemplateBinding Background}"
="{TemplateBinding BorderBrush}"
="{TemplateBinding BorderThickness}">
<Decorator x:Name="PART_ContentHost" VerticalAlignment="Top" Margin="0"/>
<Label Content="{TemplateBinding TextBoxInfo}" 
="{TemplateBinding FontSize}"

<Condition Property="HasText" Value="False"/>
<Condition Property="IsFocused" Value="True"/>
<BeginStoryboard Storyboard="{StaticResource enterGotFocus}"/>
<BeginStoryboard Storyboard="{StaticResource exitGotFocus}"/>
<Condition Property="HasText" Value="False"/>
<Condition Property="IsFocused" Value="False"/>
<Setter TargetName="background" Property="Visibility" Value="Hidden"/>

<Trigger Property="HasText" Value="True">
<Setter TargetName="Message" Property="Visibility"  Value="Hidden"/>

<Trigger Property="IsEnabled"
<Setter TargetName="background"
="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground"
="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
<Trigger Property="Width"
<Setter Property="MinWidth"

<Trigger Property="Height"
<Setter Property="MinHeight"
    The above XAML markup is really worth a detailed explanation, WPF actually supports styling, so what's styling? well, you can think of WPF styling as the CSS for XHTML elments, with styling, you can easily change the look and feel of the WPF controls by grouping the style properties of a speficied control in a common location(usually in the ResourceDictionary collection). Another thing you might note is that I define a lot of triggers inside the style element, so what's triggers? a trigger represents a specific condition for a property value, when a trigger condition is met, it wil cause the associated setter XAML statement to change a control property's value, in the above XAML markup for instance, when the "HasText" property is set to true, it will change the "Message" Label element's Visibility property to "Hidden".
    As I mentioned earlier in this post, InfoTextBox is a custom control which is derived from System.Windows.Controls.TextBox, the actual implementation of this custom control is nothing more than adding two additional dependency properties for the base TextBox control, one is TextBoxInfoProperty, and the other is HasTextProperty as the following code demonstrates:
InfoTextBox Implementation

    After all those plumbing work completes, we can use the tailer-made control anywhere as we would like:
<GroupBox Header="Comment Form" FontSize="20" Margin="10" BorderBrush="LightBlue" BorderThickness="1" Padding="10">
<Grid Margin="0">

<ColumnDefinition Width="Auto"/>
<itb:InfoTextBox TextBoxInfo="Put Your Name Here" TextWrapping="NoWrap"  Grid.Row="0" Grid.Column="0"/>
<itb:InfoTextBox TextBoxInfo="Put Your Email Address Here" TextWrapping="NoWrap"  Grid.Row="1" Grid.Column="0"/>
<itb:InfoTextBox TextBoxInfo="Put Your Comment Here" TextWrapping="NoWrap" Grid.Row="2" Grid.Column="0"/>
<Button Content="Submit" Grid.Row="3" Grid.Column="0" Width="100" Height="30" HorizontalAlignment="Left"/>

    Here is the screenshot of my functionally complete in-place editing UI:

   For complete source code, please check here.