Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

wondering about MouseBinding/KeyBinding and Command

$
0
0

Msdn does not say InputBinding.Command or MouseBinding.Command is dependency property.

Some peoples use binding like <KeyBinding Command="{Binding MyCommand}" Key="N"/>

(Refer to link

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4a852d58-9397-41e7-bee1-fc7c63d39da7/#bb0ef2d5-e116-4557-834b-fed18b060ce7 )

It works fine. Other people says it works when put it in a controltemplate

<ControlTemplate TargetType="{x:Type TabItem}">
    <Grid SnapsToDevicePixels="true">
        <Border x:Name="Bd" ...>
            <DockPanel>
                ...
            </DockPanel>
            <Border.InputBindings>
                <MouseBinding MouseAction="MiddleClick"
                              Command="{Binding CloseCommand}"/>
            </Border.InputBindings>
        </Border>
    </Grid>
    ...
</ControlTemplate>
http://efreedom.com/Question/1-2660760/Defining-InputBindings-Within-Style

But

I tried your example and it works. But when I use it to a list(for example tabcontrol)  and define a DataTemplate, code like

<DataTemplate x:Key="ClosableTabItemTemplate">
    <DockPanel Width="120">
            <DockPanel.InputBindings>
                <KeyBinding Command="{Binding Path=CloseCommand}" Key="N"/>
            </DockPanel.InputBindings>
       
        <Button
        Command="{Binding Path=CloseCommand}"
        Content="X"
        Cursor="Hand"
        DockPanel.Dock="Right"
        Focusable="False"
        FontFamily="Courier"
        FontSize="9"
        FontWeight="Bold" 
        Margin="0,1,0,0"
        Padding="0"
        VerticalContentAlignment="Bottom"
        Width="16" Height="16"
        />
          
        </DockPanel>
  </DataTemplate>

 

I always throw an exception "A 'Binding' cannot be set on the 'Command' property of type 'MouseBinding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."

My code works if I remove <DockPanel.InputBindings> section.

Thanks.

Rulin Hong


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>