2

次のようなカスタム要素があります。

    <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>

ダブルクリックすると入力フィールド(テキストボックス)に変わるラベルが必要です。したがって、次のようにダブルクリック イベントを定義しました。

    <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 になります。ダブルクリック イベントでフォーカスをテキスト ボックスに移動する方法が見つかりません。

4

2 に答える 2

1

これが役立つと思います。いくつかのコードが含まれていますが、WPF の動作を使用した再利用可能でスケーラブルなソリューションです。

于 2012-10-29T22:19:35.667 に答える
0

テキストボックスが有効または表示されているときにテキストボックスにフォーカスを設定する、テキストボックスの添付ビヘイビアーを実装できます。

于 2012-10-30T03:45:30.767 に答える