How to get a selected listviewitem from a custom WPF listview for routed event when using Elmish.WPF?

181 views Asked by At

I'm a newbie to Elmish.

The WPF binding uses the custom control as:

local:AppointmentListView.ScheduledAppointment ="AppointmentDataGrid_ScheduledAppointment" 

I have a C# custom ListView that raises the following routed event when a listview item is selected:

private void AppointmentListView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var item = (sender as ListView).SelectedItem;
            if ((IVisit)item != null)
            {
                ScheduledAppointmentEventArgs args = new ScheduledAppointmentEventArgs(ScheduledAppointmentEvent, (IVisit)item);
                RaiseEvent(args);
            }   
        }

I do not need the C# casting of "IVisit", but I DO NEED the selected listview item to be of Visit type from the F# model. That is, I need the actual object appointment key. But what I am getting is: "Elmish.WPF.ViewModel<object, object>"

The itemsSource for the ListView is defined as "AppointmentKeys" in Elmish.WPF as:

type Model =
    { AppointmentKeys: Visit.Model list
      Id: int
    }

let bindings() =[
    "AppointmentKeys"  |> Binding.subModelSeq(
                            (fun (_, m) -> m.AppointmentKeys),
                            (fun v -> v.Id),
                             Visit.bindings                            
                          )

So, the question is: In the code behind, how do I return the F# record selected by the user from the ListView?

More specifically, how is the F# code written to return the selected list view item when the "SelectedItem" comes from the code-behind binding on MouseButtonEvents?

1

There are 1 answers

0
Tyson Williams On BEST ANSWER

Don't subscribe to events with a function defined in your code-behind. Instead, convert the event into a command like this from the EventBindingsAndBehaviors sample. Then binding to a selected item from a ListView as in done in the SubModelSelectedItem sample.