作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有将项目模板作为文本框和按钮(添加)的列表框。每当单击按钮时,都会添加一个项目(文本框)。单击按钮后,我想将焦点移到第一个文本框或最近添加的文本框。我如何在WPF中做到这一点。
最佳答案
我完全同意Daniel的回答,但是让我用一些代码来澄清这个想法。
首先,让我们定义对“true”值有 react 的附加行为,并为其所有者设置焦点。
public static class FocusBehaviour
{
public static bool GetForceFocus(DependencyObject d)
{
return (bool)d.GetValue(FocusBehaviour.ForceFocusProperty);
}
public static void SetForceFocus(DependencyObject d, bool val)
{
d.SetValue(FocusBehaviour.ForceFocusProperty, val);
}
public static readonly DependencyProperty ForceFocusProperty =
DependencyProperty.RegisterAttached("ForceFocus",
typeof(bool),
typeof(FocusBehaviour),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.None,
(d, e) =>
{
if((bool)e.NewValue)
{
if (d is UIElement)
{
((UIElement)d).Focus();
}
}
}));
}
<DataTemplate>
<TextBox self:FocusBehaviour.ForceFocus="{Binding IsFocused}"/>
</DataTemplate>
public class Item : ObservableObject
{
//...
private bool _isFocused = true;
public bool IsFocused
{
get
{
return this._isFocused;
}
set
{
this._isFocused = value;
this.OnPropertyChanged("IsFocused");
}
}
}
关于wpf - WPF专注于带有控件模板的ListBox项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5576753/
我有一个 Collection View 并以这样一种方式排列它,即在屏幕的一侧有一个单独的列,并且根据焦点中的集合项替换内容。 如果对某个项目的关注超过 0.5 秒,我希望能够换出内容。 这是我目前
这是一种经常出现的情况,对我来说永远不会太容易。我想我会问其他人如何处理它。 想象一下,如果 demo=60 命令行参数的处理是这样完成的: if DemoOptionSpecified() {
我是一名优秀的程序员,十分优秀!