ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 在控件模板中设置文本框焦点

2019-07-06 13:08:17  阅读:189  来源: 互联网

标签:c wpf xaml triggers binding


我有一个控件模板,用于定义自定义浮动文本框.

它由标签,作为文本框可视边界的边框和该边框内的文本框组成.

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

我的问题是这样的:当自定义控件在UI中“标签”时,控件获取KeyboardFocus,但文本框本身没有.这会导致闪烁的光标不显示.

我需要知道如何将焦点从控件模板中的触发器传递到边框中包含的文本框,名为DisplayText.

我尝试使用FocusManager将DisplayText设置为聚焦元素,但这不起作用.

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

控制模板:

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

解决方法:

尝试将Focusable =“False”添加到Label.

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

标签:c,wpf,xaml,triggers,binding
来源: https://codeday.me/bug/20190706/1397430.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有