c# – 在控件模板中设置文本框焦点
发布时间:2020-12-15 22:24:38 所属栏目:百科 来源:网络整理
导读:我有一个控件模板,用于定义自定义浮动文本框. 它由标签,作为文本框可视边界的边框和该边框内的文本框组成. 文本框本身的边框不可见. 我的问题是这样的:当自定义控件在UI中“标签”时,控件获取KeyboardFocus,但文本框本身没有.这会导致闪烁的光标不显示. 我
我有一个控件模板,用于定义自定义浮动文本框.
它由标签,作为文本框可视边界的边框和该边框内的文本框组成. 文本框本身的边框不可见. 我的问题是这样的:当自定义控件在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,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"> <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复制到一个窗口并运行它,但显然还有一些其他的东西,我需要它才能让它工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |