How to use from one short key on active tab?
I have a window that has some tabs,in each tab i can create a new item.
I want define a short key for create new item.But i want my short Key work on active tab. For example, when Tab1 was active my short key work on create item in Tab1 or when Tab2 was active my short key work on create item in Tab2. How can i use from one short key on 开发者_高级运维active tab?There are many ways to accomplish this. The most common is to use a command. First, here's the XAML I used:
<Grid>
<TabControl Grid.Row="0"
x:Name="AppTabs">
<TabItem Header="Tab 1">
<ListBox x:Name="TabOneList" />
</TabItem>
<TabItem Header="Tab 2">
<ListBox x:Name="TabTwoList" />
</TabItem>
</TabControl>
</Grid>
Here's the code-behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// create the new item command and set it to the shortcut Ctrl + N
var newItemCommand = new RoutedUICommand("New Item", "Makes a new item on the current tab", typeof(MainWindow));
newItemCommand.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control, "Ctrl + N"));
// create the command binding and add it to the CommandBindings collection
var newItemCommandBinding = new CommandBinding(newItemCommand);
newItemCommandBinding.Executed += new ExecutedRoutedEventHandler(newItemCommandBinding_Executed);
CommandBindings.Add(newItemCommandBinding);
}
private void newItemCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
// one way to get the ListBox control from the currently selected tab
ListBox itemList = null;
if (AppTabs.SelectedIndex == 0)
itemList = this.TabOneList;
else if (AppTabs.SelectedIndex == 1)
itemList = this.TabTwoList;
if (itemList == null)
return;
itemList.Items.Add("New Item");
}
I wouldn't consider this production code, but hopefully it points you in the right direction.
精彩评论