PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/Application/GUI/Controls/EditableTextBlock.xaml

http://yet-another-music-application.googlecode.com/
XAML | 115 lines | 78 code | 11 blank | 26 comment | 0 complexity | ad201d31b990b97415ce5999e3d259d2 MD5 | raw file
  1<!--EditableTextBlock.xaml
  2	
  3	A custom textblock that can be turned into a textbox for
  4	editing. This code was originally written by Jesper Borgstrup
  5	and can be found at
  6	http://www.codeproject.com/KB/WPF/editabletextblock.aspx
  7	
  8	= = = = = = = = = =
  9	
 10	This code is part of the Stoffi Music Player Project.
 11	Visit our website at: stoffiplayer.com
 12	
 13	This program is free software; you can redistribute it and/or
 14	modify it under the terms of the GNU General Public License
 15	as published by the Free Software Foundation; either version
 16	3 of the License, or (at your option) any later version.
 17	
 18	See stoffiplayer.com/license for more information.
 19	-->
 20<UserControl x:Class="Stoffi.EditableTextBlock"
 21			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 22			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 23			 xmlns:local="clr-namespace:Stoffi"
 24			 x:Name="mainControl">
 25	<UserControl.Resources>
 26
 27		<DataTemplate x:Key="EditModeTemplate">
 28			<TextBox KeyUp="TextBox_KeyUp" 
 29					 Loaded="TextBox_Loaded" 
 30					 LostFocus="TextBox_LostFocus" 
 31					 x:Name="EditBox" 
 32					 Margin="0 0 0 0"
 33					 BorderThickness="1" 
 34					 BorderBrush="Black"
 35					 Padding="-1 -1 0 -1"/>
 36			<DataTemplate.Triggers>
 37				<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EditableTextBlock}}, Path=SimpleHover}" Value="False">
 38					<Setter TargetName="EditBox" Property="Padding" Value="-1 -1 0 1"/>
 39					<Setter TargetName="EditBox" Property="Margin" Value="0 0 0 -2"/>
 40				</DataTrigger>
 41			</DataTemplate.Triggers>
 42		</DataTemplate>
 43
 44		<DataTemplate x:Key="DisplayModeTemplate">
 45			<Border SnapsToDevicePixels="True" CornerRadius="1" VerticalAlignment="Top" Padding="0" 
 46					Margin="0,0,0,0" Name="OuterBorder" BorderThickness="0,0,1,1" BorderBrush="Transparent">
 47				<Border SnapsToDevicePixels="True" CornerRadius="1" VerticalAlignment="Top" Padding="0" 
 48						Margin="0 0 0 0" Name="InnerBorder" BorderThickness="1,1,0,0" BorderBrush="Transparent">
 49					<StackPanel>
 50						<TextBlock Text="{Binding ElementName=mainControl, Path=FormattedText}"  Name="Block"
 51							   Margin="0"
 52							   Padding="2,0,0,0"
 53							   MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" 
 54							   TextTrimming="CharacterEllipsis"
 55							   ToolTip="{Binding ElementName=mainControl, Path=FormattedText}"/>
 56					</StackPanel>
 57				</Border>
 58			</Border>
 59			<DataTemplate.Triggers>
 60
 61				<!-- simple hover  -->
 62				<MultiDataTrigger>
 63					<MultiDataTrigger.Conditions>
 64						<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Border}}, Path=IsMouseOver}" Value="True"/>
 65						<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EditableTextBlock}}, Path=ClickToEdit}" Value="True"/>
 66						<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EditableTextBlock}}, Path=SimpleHover}" Value="True"/>
 67					</MultiDataTrigger.Conditions>
 68					
 69					<!-- border -->
 70					<Setter TargetName="OuterBorder" Property="BorderBrush" Value="Gray"/>
 71					<Setter TargetName="InnerBorder" Property="BorderBrush" Value="Gray"/>
 72
 73					<!-- cursor -->
 74					<Setter Property="Cursor" Value="IBeam"/>
 75				</MultiDataTrigger>
 76
 77				<!-- non-simple hover -->
 78				<MultiDataTrigger>
 79					<MultiDataTrigger.Conditions>
 80						<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Border}}, Path=IsMouseOver}" Value="True"/>
 81						<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EditableTextBlock}}, Path=ClickToEdit}" Value="True"/>
 82						<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EditableTextBlock}}, Path=SimpleHover}" Value="False"/>
 83					</MultiDataTrigger.Conditions>
 84
 85					<!-- spacing -->
 86					<Setter TargetName="OuterBorder" Property="Margin" Value="-1,0,0,-2"/>
 87					<Setter TargetName="OuterBorder" Property="CornerRadius"  Value="2"/>
 88					<Setter TargetName="InnerBorder" Property="CornerRadius"  Value="2"/>
 89					<Setter TargetName="Block" Property="Padding" Value="3,0,0,2"/>
 90
 91					<!-- border -->
 92					<Setter TargetName="OuterBorder" Property="BorderBrush" Value="PaleTurquoise"/>
 93					<Setter TargetName="InnerBorder" Property="BorderBrush" Value="SteelBlue"/>
 94					<Setter TargetName="InnerBorder" Property="Background"  Value="White"/>
 95					<Setter TargetName="Block" Property="Foreground"  Value="Black"/>
 96
 97					<!-- cursor -->
 98					<Setter Property="Cursor" Value="IBeam"/>
 99				</MultiDataTrigger>
100				
101			</DataTemplate.Triggers>
102		</DataTemplate>
103
104		<Style TargetType="{x:Type local:EditableTextBlock}">
105			<Style.Triggers>
106				<Trigger Property="IsInEditMode" Value="True">
107					<Setter Property="ContentTemplate" Value="{StaticResource EditModeTemplate}" />
108				</Trigger>
109				<Trigger Property="IsInEditMode" Value="False">
110					<Setter Property="ContentTemplate" Value="{StaticResource DisplayModeTemplate}" />
111				</Trigger>
112			</Style.Triggers>
113		</Style>
114	</UserControl.Resources>
115</UserControl>