- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Silverlight 应用程序,它有一个带有多个 TabItem 的 TabControl。当用户选择选项卡项时,我想将焦点设置到该选项卡项中的特定控件。我该怎么做?
我尝试为 TabControl 的 SelectionChanged
事件创建一个事件处理程序,并添加以下代码:
private void tcTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tcTabs != null)
{
switch (tcTabs.SelectedIndex)
{
case 0:
txtTextBox1.Focus();
break;
case 1:
txtTextBox2.Focus();
break;
...
}
}
}
其中 txtTextBox1
和 txtTextBox2
是相关选项卡中的 TextBox 控件。
如果我在 Focus
方法调用上设置断点,当我从一个选项卡切换到另一个选项卡时,我会看到它们被调用,但当选项卡显示时控件并未聚焦。我的假设是我需要在稍后的某个时刻调用 Focus
,但我不知道何时调用它或如何调用它。
非常感谢任何帮助。
谢谢
最佳答案
TabControl 的有趣事实:每次切换选项卡时,TabItem 的子控件都会重新加载。也就是说,引发其 Loaded 事件。因此您可以在那里附加一个事件。
也就是说,我更喜欢使用 Expression SDK 中提供的触发器/操作行为,这样我就可以通过 XAML 将所有这些连接起来(对我来说,这比每次需要时都必须附加事件更具可重用性)。
如果您还没有 System.Windows.Interactivity.dll,请添加对它的引用。
使用此触发操作子类:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace SilverlightApplication1 {
public class SetFocusAction : TriggerAction<DependencyObject> {
public static readonly DependencyProperty TargetProperty =
DependencyProperty.Register( "Target", typeof( Control ), typeof( SetFocusAction ), new PropertyMetadata( null ) );
public Control Target {
get { return (Control) GetValue( TargetProperty ); }
set { SetValue( TargetProperty, value ); }
}
protected override void Invoke( object parameter ) {
if( Target != null ) {
Target.Focus();
}
}
}
}
然后像这样连接起来:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:SilverlightApplication1">
<Grid x:Name="LayoutRoot">
<sdk:TabControl>
<sdk:TabItem Header="Tab 1">
<Grid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<local:SetFocusAction Target="{Binding ElementName=tb1}"></local:SetFocusAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBox Width="200" Height="30" x:Name="tb1"></TextBox>
</Grid>
</sdk:TabItem>
<sdk:TabItem Header="Tab 2">
<Grid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<local:SetFocusAction Target="{Binding ElementName=tb2}"></local:SetFocusAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBox Width="200" Height="30" x:Name="tb2"></TextBox>
</Grid>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>
请注意,当您第一次运行包含此内容的应用程序时,Silverlight 对象本身可能没有焦点,因此您必须连接 javascript 来手动聚焦它。但是,一旦用户单击 Silverlight 应用程序(或您的 javascript 设置焦点),此操作就会完成其工作。
为了好玩,这里有一个完整的Behavior子类,它将与DataForm一起使用,并且应该适用于其他模板化控件。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace SilverlightApplication1 {
public class SetFocusBehavior : Behavior<FrameworkElement> {
public static readonly DependencyProperty TargetNameProperty =
DependencyProperty.Register( "TargetName", typeof( string ), typeof( SetFocusBehavior ), new PropertyMetadata( null ) );
private bool _setFocusOnLayoutUpdated;
public string TargetName {
get { return (string) GetValue( TargetNameProperty ); }
set { SetValue( TargetNameProperty, value ); }
}
protected override void OnAttached() {
base.OnAttached();
this.AssociatedObject.LayoutUpdated += new EventHandler( AssociatedObject_LayoutUpdated );
this.AssociatedObject.Loaded += new RoutedEventHandler( AssociatedObject_Loaded );
}
protected override void OnDetaching() {
base.OnDetaching();
this.AssociatedObject.LayoutUpdated -= new EventHandler( AssociatedObject_LayoutUpdated );
this.AssociatedObject.Loaded -= new RoutedEventHandler( AssociatedObject_Loaded );
}
private void AssociatedObject_Loaded( object sender, RoutedEventArgs e ) {
if( !FindAndSetFocus() ) {
_setFocusOnLayoutUpdated = true;
}
}
private void AssociatedObject_LayoutUpdated( object sender, EventArgs e ) {
if( _setFocusOnLayoutUpdated ) {
_setFocusOnLayoutUpdated = false;
FindAndSetFocus();
}
}
private bool FindAndSetFocus() {
var found = Find( this.AssociatedObject ) as Control;
if( found != null ) {
found.Focus();
return true;
}
return false;
}
private DependencyObject Find( DependencyObject root ) {
if( root != null ) {
if( (string) root.GetValue( FrameworkElement.NameProperty ) == TargetName ) {
return root;
}
for( int i = 0; i < VisualTreeHelper.GetChildrenCount( root ); i++ ) {
var result = Find( VisualTreeHelper.GetChild( root, i ) );
if( result != null )
return result;
}
}
return null;
}
}
}
和 XAML:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:tk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:local="clr-namespace:SilverlightApplication1">
<Grid x:Name="LayoutRoot">
<sdk:TabControl>
<sdk:TabItem Header="Tab 1">
<Grid>
<i:Interaction.Behaviors>
<local:SetFocusBehavior TargetName="tb1"></local:SetFocusBehavior>
</i:Interaction.Behaviors>
<TextBox Width="200" Height="30" x:Name="tb1"></TextBox>
</Grid>
</sdk:TabItem>
<sdk:TabItem Header="Tab 2">
<Grid>
<i:Interaction.Behaviors>
<local:SetFocusBehavior TargetName="tb2"></local:SetFocusBehavior>
</i:Interaction.Behaviors>
<tk:DataForm CurrentItem="sometext">
<tk:DataForm.EditTemplate>
<DataTemplate>
<TextBox Width="200" Height="30" x:Name="tb2"></TextBox>
</DataTemplate>
</tk:DataForm.EditTemplate>
</tk:DataForm>
</Grid>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>
关于silverlight - 选择 TabItem 时将焦点设置到 TabItem 中的 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6823830/
这个问题在这里已经有了答案: How to automatically select all text on focus in WPF TextBox? (33 个答案) How to select
我在我的项目中使用了以下模板:
我有一个非常奇怪的问题。我有多个用于用户名/密码的文本框,每个用户/密码组旁边都有一个复选框。当用户单击 CheckBox 时,如果他选中它,它旁边的用户名和密码 TextBox 将被启用,焦点将设置
我有两个带有这样的日期选择器的文本框。 $(function () { $('#value_date').datetimepicker({ format: 'MM/DD/YY
我检查了大约 60 个国家/地区的邮政编码。用户输入邮政编码,根据该国家/地区的规则进行检查。美国是个异常(exception),我可以根据已输入的其他街道地址信息自动填写值。在某些方面,它基本上是一
我们的应用程序启动几个后台进程并将它们的输出放入 TextBoxes - 每个在 TabControl 中的一个单独的 TabItem。我希望 TextBoxes 自动滚动以显示最后一个输出行,因此在
TextBlock 元素很好地处理了 LineHeight,允许文本完全显示,而无需剪切。但是,我想将其更改为 TextBox 以方便编辑文本,这就是我的麻烦开始的地方。 TextBlock 显示如下
我正在尝试“帮助”用户表单上文本输入框的用户输入百分比。 截至目前,我的输入框将任何输入转换为百分比形式。这意味着键入“6”会将值转换为 600%。如下所示 databaseViewer 是输入框从中
我有一个带有 RichEditBox 的 Windows 应用商店应用程序(编辑)和一个 Grid (边注)。 我需要始终匹配两个元素的垂直滚动位置。这样做的目的是允许用户在文档的页边空白处添加注释。
我有一个问题。下面的代码用于通过 .append() 和 .remove() 动态添加和删除文本框。我希望占位符为 textbox 的文本框中的所有数据都会内爆,并放置在我设置的名称为 textbox
我有一个文本框,其 Text 属性具有 TwoWay MultiBinding,UpdateSourceTrigger 设置为 属性已更改。第一个 Binding 是一个依赖属性 (Value),它具
它们之间有什么区别吗?谢谢 TextBox.Clear(); TextBox.Text = string.Empty; 最佳答案 在实践中:不。在内部,有。两者都以完全不同的方式清除文本。 我不敢告
这两种方法有什么区别? 一个比另一个更有效率吗? 我在想 AppendText() 可能使用类似于 StringBuilder 的方法,即它使用自己的缓存而不是每次都创建和附加一个新字符串,是这样吗?
我有一个文本框来显示一个很长的 Base64 字符串。 TextBox.Multline = true 和 TextBox.WordWrap = true。 此问题是由文本框本身的自动字边界检测引起的
在 this document在描述 Windows 10 UWP 应用程序的生命周期时,它指出: Users now expect your app to remember its state as
我需要检查如果我的 TextBox (txtmoney) 为空或等于 0,则不允许按 0 键键盘或数字 0。 我该怎么做? 最佳答案 Before implementing please refer
我有一个显示一些 TextBoxes 的 ListView。对于每个 TextBox,我都会捕获一个 SelectionChanged 事件。 我的 XAML 文件看起来像这样:
当我单击(或双击)Access 表单的文本框时,我需要选择其中的所有文本。我尝试了以下代码,但没有成功: Me.txt_CompraPreco.SelStart = 0 Me.txt_CompraPr
我有一个 ListView(在更新面板中)绑定(bind)到一个 ObjectDataSource(连接到一个 NHibernate 对象)并且在 EditItemTemplate 我使用 Text=
我正在尝试使用 D3 绘制多条交互式线条。单击每一行时,应根据当前鼠标坐标显示一个 float 文本框,并等待输入更新行坐标。更改文本框后,该行应自动更新,文本框应消失。有人可以看看我的代码并找出问题
我是一名优秀的程序员,十分优秀!