关闭 AutoCompleteBox 会使浏览器失去焦点
我扩展了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用什么版本的 Silverlight?它对我来说适用于 V4。
What version of Silverlight are you using? It works for me with V4.