简体   繁体   中英

How to write a WPF user control in F#?

Can a WPF user control be written in F#?

Lets say I have a standard WPF/C# user control as:

public class DataGridAnnotationControl : UserControl
    {
        static DataGridAnnotationControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridAnnotationControl), new FrameworkPropertyMetadata(typeof(DataGridAnnotationControl)));
        }
        
        public DataGridAnnotationControl()
        {
            BorderBrush = Brushes.Black;
            Background = Brushes.AliceBlue;
            BorderThickness = new Thickness(20, 20, 20, 20);
        }
        
        public string LastName
        {
            get { return (string)GetValue(LastNameProperty); }
            set { SetValue(LastNameProperty, value); }
        }
        
        public static readonly DependencyProperty LastNameProperty =
            DependencyProperty.Register("LastName", typeof(string), typeof(DataGridAnnotationControl), new PropertyMetadata(string.Empty));
        
}

How is this coded in F#?

TIA

In general, creating the user control in F# (without a library) will typically be quite different than in C#.

The main issue is you can't use partial classes, so the designer will not function. Even if you ditch the designer, the typical workflow with XAML files does not work properly. To do this in "pure" F#, you typically need to write the UI in code vs doing it in XAML and allowing the generated InitializeComponent() method to wire things together.

However, one way to get there in a more "natural" method is to use FsXaml . It allows you to write user controls directly which become usable in a similar way to C# developed ones. This is done via a type provider and overriding the default information .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM