8,737
社区成员
发帖
与我相关
我的任务
分享
<controls:TabControl x:Name="tbC" Grid.Row="1" >
<controls:TabItem Header="基本信息">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="用户组名:" />
<TextBox Grid.Row="0" Grid.Column="1" Name="tbName" />
<CheckBox x:Name="cb" Grid.Row="1" Content="test" />
</Grid>
</controls:TabItem>
<controls:TabItem Header="客户">
<Grid Background="AliceBlue">
<ListBox x:Name="lstCustomers">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" Tag="{Binding ID}" IsChecked="{Binding IsSelected, Mode=TwoWay}"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:TabItem>
</controls:TabControl>
public static class IMEHooker
{
private static Dictionary<Control, Control> IMEControls { get; set; }
static IMEHooker()
{
IMEControls = new Dictionary<Control, Control>();
}
/// <summary>
///Register an input control and a focus shift control
///Call this method in load event
/// </summary>
/// <param name="input">the ctrl which will receive the IME input</param>
/// <param name="foucs">the ctrl which can receive focus</param>
public static void RegisterControl(Control input, Control focus)
{
//If ctrl in dictionary just return
if (IMEControls.ContainsKey(input)) return;
//add ctrl to dictionary
IMEControls[input] = focus;
//register got focus event
input.GotFocus += OnGotFocus;
}
//Inversed process of register control method
public static void UnregisterControl(Control input)
{
if (!IMEControls.ContainsKey(input)) return;
IMEControls.Remove(input);
input.GotFocus -= OnGotFocus;
}
static void OnGotFocus(object sender, RoutedEventArgs e)
{
Control input = sender as Control;
if (input != null)
{
Control focus;
if (IMEControls.TryGetValue(input, out focus))
{
//unregister the event to avoid the call over stack
input.GotFocus -= OnGotFocus;
//let's shift the focus to the predefined ctrl
focus.Focus();
//focus again, focus event will not be fired.
input.Focus();
//register got focus event again
input.GotFocus += (OnGotFocus);
}
}
}
}