【问题标题】:NumericUpDown - Focus not setNumericUpDown - 未设置焦点
【发布时间】: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


    【解决方案1】:

    我从官方支持那里得到了一些信息。

    这已经修复了。该修复程序包含在 v3.1 中。 (您可以在此处查看讨论线程:https://wpftoolkit.codeplex.com/discussions/658785

    要修复它,请进入文件:

    Xceed.wpf.Toolkit/NumericUpDown/Themes/Aero2.NormalColor.xaml (适用于 Windows8 及更高版本)

    Xceed.wpf.Toolkit/NumericUpDown/Themes/Generic.xaml (对于其他 Windows)

    在“NumericUpDown”的样式中

    a) 替换

    <Setter Property="Focusable" Value="False" /> 
    

    <Setter Property="IsTabStop" Value="False" /> 
    

    b) 在“PART_TextBox”中

    替换

    IsTabStop="{TemplateBinding IsTabStop}" 
    

    IsTabStop="True"
    

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多