









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">
<Setter.Value>
<ControlTemplate TargetType="{x:Type itb:InfoTextBox}">
<Grid Margin="0">
<Border Name="background"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Decorator x:Name="PART_ContentHost" VerticalAlignment="Top" Margin="0"/>
</Border>
<Label Content="{TemplateBinding TextBoxInfo}"
x:Name="Message"
Foreground="Gray"
Opacity="0.9"
FontSize="{TemplateBinding FontSize}"
FontStyle="Italic"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="0"/>
</Grid>
<ControlTemplate.Triggers><MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasText" Value="False"/>
<Condition Property="IsFocused" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource enterGotFocus}"/>
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource exitGotFocus}"/>
</MultiTrigger.ExitActions>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasText" Value="False"/>
<Condition Property="IsFocused" Value="False"/>
</MultiTrigger.Conditions>
<Setter TargetName="background" Property="Visibility" Value="Hidden"/>
</MultiTrigger><Trigger Property="HasText" Value="True">
<Setter TargetName="Message" Property="Visibility" Value="Hidden"/>
</Trigger><Trigger Property="IsEnabled"
Value="false">
<Setter TargetName="background"
Property="Background"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="Width"
Value="Auto">
<Setter Property="MinWidth"
Value="100"/>
</Trigger><Trigger Property="Height"
Value="Auto">
<Setter Property="MinHeight"
Value="20"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
InfoTextBox Implementation
<GroupBox Header="Comment Form" FontSize="20" Margin="10" BorderBrush="LightBlue" BorderThickness="1" Padding="10">
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions><Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<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"/>
</Grid>
</GroupBox>
For complete source code, please check here.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。