【问题标题】:Set Textbox Focus within a Control Template在控件模板中设置文本框焦点
【发布时间】:2015-12-18 20:28:09
【问题描述】:

我有一个定义自定义浮动文本框的控件模板。

它由一个标签、一个用作文本框视觉边界的边框和一个位于该边框内的文本框组成。

文本框本身的边框不可见。

我的问题是这样的:当自定义控件在 UI 中被“选项卡化”时,控件会得到KeyboardFocus,但Textbox 本身没有。这会导致闪烁的光标不显示。

我需要知道如何从控件模板中的触发器将焦点传递给包含在边框内的Textbox,名为DisplayText

我尝试使用FocusManagerDisplayText 设置为焦点元素,但这不起作用。

任何想法、想法或建议将不胜感激。如果您需要更多信息,请告诉我。

控制模板:

<Grid SnapsToDevicePixels="True"
    UseLayoutRounding="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="floatingLabel"
               Template="{DynamicResource LabelControlTemplate1}"
               Content="{Binding LabelText, RelativeSource={RelativeSource Mode=TemplatedParent}}"
               IsHitTestVisible="False"
               Panel.ZIndex="2"
               Background="White"
               Height="15"
               VerticalContentAlignment="Center"
               Padding="3,0,3,0"
               HorizontalAlignment="Left"
               FontFamily="Segoe UI"
               FontSize="{Binding LabelFontSize, RelativeSource={RelativeSource TemplatedParent}}"
               Foreground="{DynamicResource FloatingLabelTextBox.Label.Foreground}"
               VerticalAlignment="Center">

        <Label.Tag>
            <sys:Double>0.0</sys:Double>
        </Label.Tag>

        <Label.Margin>
            <MultiBinding Converter="{StaticResource floatingLabelMarginConverter}">
                <Binding Path="Tag"
                             RelativeSource="{RelativeSource Self}" />
                <Binding ElementName="Border"
                             Path="ActualHeight" />
            </MultiBinding>
        </Label.Margin>
    </Label>

    <Border x:Name="Border"
                Height="{Binding TextBoxHeight, RelativeSource={RelativeSource TemplatedParent}}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="{TemplateBinding Background}"
                CornerRadius="3"
                SnapsToDevicePixels="True"
                Panel.ZIndex="0"
                VerticalAlignment="Bottom">

        <Grid x:Name="GridContainer" Width="{Binding ElementName=Border, Path=ActualWidth}" Margin="10,0,0,0">
            <TextBox x:Name="DisplayText" 
                     Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=FormattedPhoneNumber, StringFormat={}{0:(###)###-####}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                     VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                     FontFamily="{TemplateBinding FontFamily}"
                     FontSize="{TemplateBinding FontSize}"
                     FontWeight="{TemplateBinding FontWeight}"
                     Foreground="{TemplateBinding Foreground}"
                     Width="{Binding ElementName=Border, Path=ActualWidth}">
                <TextBox.Template>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <ScrollViewer x:Name="PART_ContentHost" 
                                      HorizontalAlignment="Stretch" 
                                      Margin="{TemplateBinding Padding}" 
                                      Uid="ScrollViewer_1" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>
        </Grid>
    </Border>
</Grid>

触发器:

<Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=DisplayText}" />
                    </Trigger>

【问题讨论】:

  • 你解决了这个问题吗?

标签: c# wpf xaml binding triggers


【解决方案1】:

尝试将Focusable="False" 添加到Label

我试图将您的 XAML 复制到一个窗口中并运行它,但显然我还需要一堆其他的东西才能让它工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多