关闭 AutoCompleteBox 会使浏览器失去焦点

发布于 2024-11-04 04:58:13 字数 2311 浏览 1 评论 0原文

我扩展了 Silverlight 的 AutoCompleteBox 并重写了 OnDropDownClosed 事件处理程序。这按预期工作,只是一旦 DropDown 关闭,组件就会失去浏览器的焦点。

为了保留它,我必须改变什么?

这是我的代码:

namespace ITPole.Sphere.Application.Core.Controls
{

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    public class CustomCompleteBox : AutoCompleteBox
    {    
        public static readonly DependencyProperty SelectedAtCloseProperty =
            DependencyProperty.Register(
                "SelectedAtClose", typeof(object), typeof(CustomCompleteBox), new PropertyMetadata(null));

        public object SelectedAtClose
        {
            get
            {
                return this.GetValue(SelectedAtCloseProperty);
            }

            set
            {
                this.SetValue(SelectedAtCloseProperty, value);
            }
        }

        protected override void OnDropDownClosed(RoutedPropertyChangedEventArgs<bool> e)
        {
            base.OnDropDownClosed(e);
            this.SelectedAtClose = this.SelectedItem;

        }

        protected override void OnTextChanged(RoutedEventArgs e)
        {
            base.OnTextChanged(e);

            if (string.IsNullOrEmpty(this.Text))
            {
                this.SetValue(SelectedAtCloseProperty, null);
            }
        }

    }
}

以及 xaml 中的用法:

<Controls1:CustomCompleteBox x:Name="portfolioAutoCompleteBox"
                             Grid.Column="1"
                             Grid.ColumnSpan="2"
                             Grid.Row="1"
                             Margin="2"
                             DataContext="{Binding Portfolio}"
                             Style="{StaticResource DefaultAutoCompleteBoxStyle}"
                             ItemTemplate="{StaticResource DescriptionItemTemplate}"
                             ValueMemberBinding="{Binding Description, Mode=TwoWay}"
                             SelectedAtClose="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay}"
                             ItemsSource="{Binding Values}"
                             Text="{Binding Text, Mode=TwoWay}"
                             Behaviors:AutoCompleteBoxBehaviors.PopulatingCommand="{Binding PopulationCommand}" />

I've extended an the AutoCompleteBox of Silverlight and overridden the OnDropDownClosed event handler. This works as expected except that the component looses the focus to the Browser once the DropDown is closed.

What do I have to change in order to keep it?

Here's my code:

namespace ITPole.Sphere.Application.Core.Controls
{

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    public class CustomCompleteBox : AutoCompleteBox
    {    
        public static readonly DependencyProperty SelectedAtCloseProperty =
            DependencyProperty.Register(
                "SelectedAtClose", typeof(object), typeof(CustomCompleteBox), new PropertyMetadata(null));

        public object SelectedAtClose
        {
            get
            {
                return this.GetValue(SelectedAtCloseProperty);
            }

            set
            {
                this.SetValue(SelectedAtCloseProperty, value);
            }
        }

        protected override void OnDropDownClosed(RoutedPropertyChangedEventArgs<bool> e)
        {
            base.OnDropDownClosed(e);
            this.SelectedAtClose = this.SelectedItem;

        }

        protected override void OnTextChanged(RoutedEventArgs e)
        {
            base.OnTextChanged(e);

            if (string.IsNullOrEmpty(this.Text))
            {
                this.SetValue(SelectedAtCloseProperty, null);
            }
        }

    }
}

And the usage in xaml:

<Controls1:CustomCompleteBox x:Name="portfolioAutoCompleteBox"
                             Grid.Column="1"
                             Grid.ColumnSpan="2"
                             Grid.Row="1"
                             Margin="2"
                             DataContext="{Binding Portfolio}"
                             Style="{StaticResource DefaultAutoCompleteBoxStyle}"
                             ItemTemplate="{StaticResource DescriptionItemTemplate}"
                             ValueMemberBinding="{Binding Description, Mode=TwoWay}"
                             SelectedAtClose="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay}"
                             ItemsSource="{Binding Values}"
                             Text="{Binding Text, Mode=TwoWay}"
                             Behaviors:AutoCompleteBoxBehaviors.PopulatingCommand="{Binding PopulationCommand}" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-11-11 04:58:13

您使用什么版本的 Silverlight?它对我来说适用于 V4。

What version of Silverlight are you using? It works for me with V4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文