How is the C# Delegate Action written in F#?

How to implement a C# Action in F#?

I have the following code in C# code-behind:

public MainWindow()
        {
            InitializeComponent();
            ViewModel = new ViewModel();
            DataContext = ViewModel;
        }

        private void ListView_PreviewMouseLeftButtonUp(object _, MouseButtonEventArgs e)
        {
            _closeAdorner();
        
            // listView here equals object _  
            var listView = (ListView)e.Source;
            var grid = (Grid)listView.Parent;
            var selecteditem = (InnerRow)listView.SelectedItem;
            ViewModel.Visit = selecteditem;
            ViewModel.LastName = selecteditem.LastName;
        
            var adornerLayer = AdornerLayer.GetAdornerLayer(grid);
            if (adornerLayer == null)
                throw new ArgumentException("datagrid does not have have an adorner layer");

            var adorner = new DataGridAnnotationAdorner(grid);
            adornerLayer.Add(adorner);
        
           _closeAdorner = () => adornerLayer.Remove(adorner);
        }

I am attempting to translate this into F#:

let handlePreviewMouseLeftButtonUp (obj: obj) (a, c) =
      let e = (obj :?> MouseButtonEventArgs)
      let listView = e.Source :?> ListView   // This is the ListView control that was clicked.
      let grid = listView.Parent :?> Grid
          
      let selectedItem = c.InnerRows |> List.filter (fun r -> Some r.Id = c.SelectedInnerRow) |> List.head
    
      let adorner = DataGridAdorner(grid)

      let installAdorner =
        let adornerLayer = AdornerLayer.GetAdornerLayer(grid)
        if (adornerLayer.GetAdorners = []) then adornerLayer.Add(adorner) else adornerLayer.Remove(adorner)
 

The last line: if (adornerLayer.GetAdorners = []) then adornerLayer.Add(adorner) else adornerLayer.Remove(adorner)

clearly does not compile and is not correct. How is the C# _closeAdorner written to have the same function in F#?

Thank you.

TIA



from Recent Questions - Stack Overflow https://ift.tt/37OCocj
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation