潜移默化学会WPF(转载篇)--屏幕显示Label,鼠标移上去变成textBox

时间:2022-07-20 03:27:37
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Window.Resources>
        <Style x:Key="LabelTemp" TargetType="Label">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Label">
                        <Grid >
                            <TextBox x:Name="tb" Text="{TemplateBinding Content}" Visibility="Hidden"  Width="40" Height="20" 
                                      HorizontalAlignment="Center" VerticalAlignment="Top"  FontSize="12"></TextBox>
                            <Label x:Name="labReal" Content="{TemplateBinding Content}" Width="40" Height="30" 
                                  HorizontalAlignment="Center" VerticalAlignment="Top"  FontSize="12" ></Label>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="tb" Property="Visibility" Value="Visible">
                                </Setter>
                                <Setter TargetName="labReal" Property="Visibility" Value="Hidden"></Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="false">                                
                                <Setter TargetName="labReal" Property="Content" Value="{Binding ElementName=tb,Path=Text}"></Setter>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="104*" />
            <ColumnDefinition Width="174*" />
        </Grid.ColumnDefinitions>
        <Label Name="lab" Style="{StaticResource LabelTemp}" VerticalAlignment="Top">Demo Label
        </Label>
    </Grid>
</Window>

原文地址  http://topic.csdn.net/u/20090324/14/349de2ff-f12e-4d7a-8a37-ca3318b63b51.html