1

I can't tell where my problem is - I suspect that I'm missing some very basic technique.

The following has problems with tabbing and with commands. I'm trying some basic stuff - use a menu, toolbar and general-data input 'controls'.

After application startup, I can only tab to the MainToolbar - which is okay. The 'OpenProjectCommand' invokes a method that fills the general-data with stuff. My expectation is that I can tab into general-data afterwards - but it doesn't tab there. Focus remains in the Toolbar, switching between the open and close buttons.

Secondly, after application startup, the key bindings for the shortcut keys work - which is okay. After invoking the OpenProjectCommand method, I click into ProjectData (since I can't tab there) and then only CTLR-O works. CTRL-C does not work. If I move focus back to the Menu or Toolbar, CTRL-C works.

I'm using Prism/DelegateCommand. Using Debug, I never reach the canCloseProject() method for the cases when the code doesn't work - so I can't tell in what state the internals are in.

Is there a way to set a breakpoint for keyboard events? (without adding a bunch of code)

Or is it just that I'm missing a detail and am too blind/too new to WPF/Prism to see it?

Here's the code:

app.xaml

<Application x:Class="TestMenu.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

         StartupUri="View/MainWindow.xaml">
    <Application.Resources>         
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="TestMenu.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:view="clr-namespace:TestMenu.View"
    xmlns:viewmodel="clr-namespace:TestMenu.ViewModel"

    Title="{Binding MyTitle}" Height="350" Width="525" WindowStartupLocation="CenterScreen" >

    <StackPanel>
        <view:MainMenuView />
        <view:MainToolbarView />
        <view:MainProjectDataView />
    </StackPanel>
    <Window.InputBindings>
        <KeyBinding Key="O" Modifiers="Control" Command="{Binding Path=OpenProjectCommand}" />
        <KeyBinding Key="C" Modifiers="Control" Command="{Binding Path=CloseProjectCommand}" />
    </Window.InputBindings>
    <Window.DataContext>
        <viewmodel:MainWindowViewModel />
    </Window.DataContext>    
</Window>

MainMenuView.xaml

<UserControl x:Class="TestMenu.View.MainMenuView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cmd="clr-namespace:TestMenu.ViewModel"            
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel   Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <Menu IsMainMenu="True" Height="20" Visibility="Visible">
            <MenuItem Header="_Project" MaxWidth="50">
                <MenuItem Name="Project_Open" Header="Open"  ToolTip="Open a project."  Command="{Binding OpenProjectCommand}" InputGestureText="CTRL+O" />
                <Separator></Separator>
                <MenuItem Name="Project_Close" Header="Close" ToolTip="Close the project." Command="{Binding CloseProjectCommand}" InputGestureText="CTRL+C" />
            </MenuItem>
        </Menu>
    </StackPanel>
</UserControl>

MainToolbarView.xaml

<UserControl x:Class="TestMenu.View.MainToolbarView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cmd="clr-namespace:TestMenu.ViewModel"

         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Width="700">

    <UserControl.Resources>
        <Style x:Key="ImageEnabled" TargetType="Image">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Opacity" Value="0.25"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ToolBar HorizontalAlignment="Left">
            <Button Name="Open" ToolTip="Öffnet ein Projekt." Command="{Binding OpenProjectCommand}">
            <Image Source="/Images\Open.png" Height="32" Width="32" OpacityMask="Black" />
            </Button>
            <Button Name="Save" ToolTip="Speichert das aktuelle Projekt." Command="{Binding CloseProjectCommand}">
                <Image Source="/Images\Save.jpg" Height="32" Width="32" Style="{StaticResource ImageEnabled}" />
            </Button>
        </ToolBar>
    </StackPanel>
</UserControl>

MainProjectDataView.xaml

<UserControl x:Class="TestMenu.View.MainProjectDataView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

         mc:Ignorable="d" >
    <UserControl.InputBindings>
        <KeyBinding Key="O" Modifiers="Control" Command="{Binding Path=viewmodels.OpenProjectCommand}" />
        <KeyBinding Key="C" Modifiers="Control" Command="{Binding Path=viewmodels.CloseProjectCommand}" />
    </UserControl.InputBindings>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Width="Auto" LastChildFill="True">
            <Label Content="Project:" Height="28" Name="Projectlabel" />
            <TextBox Height="23" Name="ProjecttextBox" Width="Auto" Text="{Binding MyTitle}"/>
        </DockPanel>
        <TextBox Grid.Row="1" AcceptsReturn="True" Text="{Binding MyTextData}"></TextBox>
        <DataGrid Name="dgMainProjectDataGrid" Grid.Row="2" 
              ItemsSource="{Binding MainProjectDataTable}" 
              AutoGenerateColumns="True"
              SelectionMode="Extended"
              SelectionUnit="Cell"
              FrozenColumnCount="1"
              IsSynchronizedWithCurrentItem="True"
              CanUserAddRows="False"/>
    </Grid>        
</UserControl>
infowanna
  • 169
  • 11
  • I found and was finally able to download the wpf 'snoop' tool. (http://snoopwpf.codeplex.com/). If I'm reading it correctly, it indicates that when Focus is in the 'MainProjectDataView' ctrl-O is handled by the MainWindow (which is what I expect) but that ctrl-c is handled by the text box. Ctrl-C is probably a bad choice - can it be that it's handled by the text box? Argh! Now I see it is - how stupid of me. How do I override Keyboard behavior in a 'native' WPF control? Do I really want to override ctrl-c in the TextBox/datagrid?? – infowanna Jan 13 '15 at 11:16
  • I found out how to disable Event handling here: http://stackoverflow.com/questions/12941707/keybinding-in-usercontrol-doesnt-work-when-textbox-has-the-focus ctrl-c in the context of text controls is copy, so I won't use it in that context. For me, this is a case of 'using new Technology blinds the obvious (untbto - vocalizing probably sounds fairly rude). – infowanna Jan 13 '15 at 11:58
  • It could be that this could/should be rated as a duplicate question - should I go ahead and delete it? – infowanna Jan 13 '15 at 12:08

0 Answers0