none
How to add a MouseBinding to the InputBindings of a ListViewItem

    Question

  • I have a ListView that has a data bound ItemsSource.  When I double click on a ListViewItem, I would like to invoke ApplicationCommands.Open.

    I am pretty sure this is the xaml I want to use but I don't know where to put it.

    <?????.InputBindings>
      <MouseBinding Gesture="LeftDoubleClick" Command="ApplicationCommands.Open" />
    </?????.InputBindings>
    
    

    Does this go in a control template applied by a style?  If so which one and how do I do this without having to rewrite the original default template?

    I already have a solution that handles the MouseDoubleClick event of the ListViewItem using a style but I would like to do this without code behind, xaml only.

    Monday, July 19, 2010 6:17 PM

Answers

  • Hi Chris,

    Unfortunately, the ListViewItem can not process the Command. Although we can create some Attached Properties to set the command on the ListViewItem (Please refer to this blog which introduces a solution to add an attched command for the Grid panel: http://sachabarber.net/?p=514), it is too complex. So that, I see the solution to handle the MouseDoubleClick event is simple.

    Below is a solution to use attached properties: http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2010/20100720%5E_AttachedPropertiesCommands.zip

    Sincerely,

    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com

    ------------------------------------------------------

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, July 20, 2010 6:56 AM
    Moderator

All replies

  • Hi Chris,

    Unfortunately, the ListViewItem can not process the Command. Although we can create some Attached Properties to set the command on the ListViewItem (Please refer to this blog which introduces a solution to add an attched command for the Grid panel: http://sachabarber.net/?p=514), it is too complex. So that, I see the solution to handle the MouseDoubleClick event is simple.

    Below is a solution to use attached properties: http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2010/20100720%5E_AttachedPropertiesCommands.zip

    Sincerely,

    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com

    ------------------------------------------------------

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, July 20, 2010 6:56 AM
    Moderator
  • I think that I have found part of the solution that I am looking for.

        <ListView x:Name="lvwBreed" ItemsSource="{Binding}" SelectionMode="Single">
          <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="ListViewItem">
                    <GridViewRowPresenter>
                      <GridViewRowPresenter.InputBindings>
                        <MouseBinding Gesture="LeftDoubleClick" Command="Open" />
                      </GridViewRowPresenter.InputBindings>
                    </GridViewRowPresenter>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </ListView.ItemContainerStyle>
          <ListView.View>
            <GridView>
              <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
            </GridView>        
          </ListView.View>
        </ListView>
    
    However, I have lost the highlighting of selected items.  I am grappling with how to fix that and wondering what else I may have lost.  Did I leave out a TemplateBinding?
    Tuesday, July 20, 2010 1:39 PM
  • No, I see now that I am in the situation that I was trying to avoid where I am rewriting the default template just for the sake of getting the InputBindings in place.
    Tuesday, July 20, 2010 3:52 PM
  • Hi Chris,

    Yes, as you said, since it has rewrited the default template and the triggers can not work for the highlighting. In generally, we can get the default template/style first by some tools and modify it to what we want. Below is some tools can help us:

    Microsoft Blend 3 or 4 : http://www.microsoft.com/expression/try-it/Default.aspx
    Third part tool - ShowMeTheTemplate : http://www.sellsbrothers.com/Posts/Details/2091
    Third part tool - stylesnooper : http://blog.wpfwonderland.com/2007/01/02/wpf-tools-stylesnooper/

    Below is a sample that I modified from the default template of the ListViewItem

      <ListView x:Name="lvwBreed" ItemsSource="{Binding}" SelectionMode="Single">
       <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
         <Setter Property="Template">
          <Setter.Value>
           <ControlTemplate TargetType="ListViewItem">
            <Border Name="Bd">
             <GridViewRowPresenter>
              <GridViewRowPresenter.CommandBindings>
               <CommandBinding Command="ApplicationCommands.Open" Executed="CommandBinding_Executed"/>
              </GridViewRowPresenter.CommandBindings>
              <GridViewRowPresenter.InputBindings>
               <MouseBinding Gesture="LeftDoubleClick" Command="ApplicationCommands.Open" />
              </GridViewRowPresenter.InputBindings>
             </GridViewRowPresenter>
            </Border>
            <ControlTemplate.Triggers>
             <Trigger Property="Selector.IsSelected" Value="True">
              <Setter Property="Panel.Background" TargetName="Bd"
                  Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
              <Setter Property="TextElement.Foreground"
                  Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
             </Trigger>
             <MultiTrigger>
              <MultiTrigger.Conditions>
               <Condition Property="Selector.IsSelected" Value="True"/>
               <Condition Property="Selector.IsSelectionActive" Value="False"/>
              </MultiTrigger.Conditions>
              <Setter Property="Panel.Background" TargetName="Bd"
                  Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
              <Setter Property="TextElement.Foreground"
                  Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
             </MultiTrigger>
             <Trigger Property="UIElement.IsEnabled" Value="False">
              <Setter Property="TextElement.Foreground"
                  Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
             </Trigger>
            </ControlTemplate.Triggers>
           </ControlTemplate>
          </Setter.Value>
         </Setter>
        </Style>
       </ListView.ItemContainerStyle>
       <ListView.View>
        <GridView>
         <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        </GridView>
       </ListView.View>
      </ListView>
    

    Hope this helps.

    Sincerely,

    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com

    ------------------------------------------------------

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, July 21, 2010 3:48 AM
    Moderator
  • Hi Chris,

    How about your problem? If you need helkp, please feel free to let me know.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, July 27, 2010 1:26 AM
    Moderator
  • Here's the way I do it.. I'm using strings in my ItemsSource, but you can easily apply the data template to whatever you want and it'll work.  The idea is to use StackPanels in your datatemplate so you can handle the input gesture there.

     

          <ListView Grid.Row="1" ItemsSource="{Binding AvailableColumns}" SelectedValue="{Binding SelectedAvailableColumn}">
            <ListView.Resources>
              <DataTemplate DataType="{x:Type clrType:String}">
                <StackPanel>
                  <StackPanel.InputBindings>
                    <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding AddSelectedColumnCommand}"/>
                  </StackPanel.InputBindings>
                  <TextBlock Text="{Binding}"/>
                </StackPanel>
              </DataTemplate>
            </ListView.Resources>
          </ListView>
    

     

    Friday, January 28, 2011 11:11 PM