我扩展了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}" />
答案 0 :(得分:0)
您使用的是什么版本的Silverlight?它适用于V4。