首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XAML中内部元素的手动焦点

问XAML中内部元素的手动焦点
EN

Stack Overflow用户
提问于 2012-10-30 05:59:02
回答 2查看 606关注 0票数 2

我有一个自定义元素,如下所示:

代码语言:javascript
复制
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Grid>
                    <TextBox Name="textBox"
                         Grid.ZIndex="1"
                         Padding="0,3,0,0"
                         Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Label}}, Path=Content, UpdateSourceTrigger=PropertyChanged}"
                         Opacity="0"
                         IsEnabled="False"
                         Focusable="True"
                         />


                    <Border Name="boxBorder"  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}" Padding="3,0,0,0" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Grid>

我想要一个标签,你可以双击,然后变成一个输入字段(文本框)。因此,我使用以下命令定义了一个双击事件:

代码语言:javascript
复制
    <EventTrigger RoutedEvent="MouseDoubleClick">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="textBox"
                                                 Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                <BooleanAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="textBox"
                                                                Storyboard.TargetProperty="IsEnabled">
                                    <DiscreteBooleanKeyFrame Value="True" KeyTime="0" />
                                </BooleanAnimationUsingKeyFrames>

                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>

双击可以很好地工作,但您必须执行三次单击才能将焦点置于文本框中。双击只会将不透明度设置为1。我还没有找到一种方法来将双击事件的焦点移动到文本框中。

EN

回答 2

Stack Overflow用户

发布于 2012-10-30 06:19:36

我觉得this能帮上忙。它包含一些代码,但它是一个使用WPF行为的可重用和可伸缩的解决方案。

票数 1
EN

Stack Overflow用户

发布于 2012-10-30 11:45:31

您可以实现文本框的附加行为,当文本框处于启用或可见状态时,该行为将焦点设置在文本框上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13130273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档