【发布时间】:2016-10-16 08:53:44
【问题描述】:
我正在使用 NumericUpDown 控件(来自 WPFs 扩展工具包 2.9 版),我正在尝试通过附加属性将焦点设置在它上。
我的 XAML
<xctk:DecimalUpDown FormatString="F5"
wbui:FocusExtension.IsFocused="{Binding Path=IsFocusedMenge, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Value="{Binding Path=Menge, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
这是我的 FocusExtensiion.IsFocues
private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)d;
if (e.OldValue == null)
{
fe.GotFocus += FrameworkElement_GotFocus;
fe.LostFocus += FrameworkElement_LostFocus;
}
if (!fe.IsVisible)
{
fe.IsVisibleChanged += new DependencyPropertyChangedEventHandler(FrameworkElement_IsVisibleChanged);
}
if ((bool)e.NewValue)
{
fe.Focus(); // will be called
}
}
当我将我的属性 IsFocusedMenge 设置为 true 时,将不会设置焦点。当我在那里设置断点时,将调用代码行 fe.Focus()。
我在这里找到了另一个话题(How to set focus on NumericUpDown control?),但是当我设置这个属性Focusable=true时,调用fe.Focus()方法时会得到一个StackOverFlowException。
有什么想法吗?谢谢。
更新
还尝试将事件添加到网格中,以在 View/UserControl 中设置焦点...但没有成功。
private void GridMenge_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
this.Menge.Focus();
}
}
焦点仍未设置(Property Focusable 设置为 True/False - 无更改)
【问题讨论】:
标签: wpf focus wpftoolkit numericupdown