etwas77
etwas77

Reputation: 514

WPF, MVVM, EventBehaviourFactory in ListView, binding event to command

i am basically trying to follow http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html strategy, to bind an event to command.

i have a ListView, where i would like to start an instance of ICommand on double click on a row. a ListView or ListBoxItem on it do not have a Command Property. this problem (so it seems) was solved by using attached property, but i personally still cannot figure out how to use it on ListView.

I have a collection of "Signals", each has a property "Name". Below is a part of my xaml.

            <ListView ItemsSource="{Binding Signals}" >
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="{models:ListBoxItemBehaviour.DoubleClickCommand}" Value="{Binding Command1}" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListView.View>
                   <GridView>
                      <GridViewColumn Header="Signal" DisplayMemberBinding="{Binding Name}" />
                   </GridView>
               </ListView.View>
           </ListView>

this does not work. ListBoxItemBehaviour is an implementation of a behaviour, following link above, hooked on ListBoxItem.MouseDoubleClickEvent event.

i suspect the error above is small, but not obvious to me. any suggestions?

first error is: "Nested types are not supported:"..

Upvotes: 1

Views: 659

Answers (2)

etwas77
etwas77

Reputation: 514

well, since i was not able to find a proper solution, but work is work, i sacrificed purity of MVVM pattern over workability.

added this event to xaml

<EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" /> 

and handler to code behind

    protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
    {
        MyViewModel mvm = this.DataContext as MyViewModel;

        mvm.MyCommand.Execute();
    }

thus double click on item in the viewlist will trigger the command. not beautiful, but works. if i find the proper pure MVVM solution, i will post it.

Upvotes: 0

Tomtom
Tomtom

Reputation: 9384

In a ListView you can use the InputBindings to get the Mouse- and Keyboard-Inputs on it.

To bind the MouseDoubleClick to a Command you can use

<ListView.InputBindings>
   <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ListViewDoubleClickCommand}"/>
</ListView.InputBindings>

Upvotes: 1

Related Questions