1

I have a function (let's call it StringFunction) that periodically runs and I would like to automatically update a TextBlock in my GUI once the function returns.

There are many implementations of INotifyPropertyChanged, which is presumably what I would have to use here. However there are no minimal examples of how to use the implementation in F#. In particular, I cannot find examples that address more basic issues such as what to put in the XAML (<TextBlock Text={Binding StringFunction}>?), and how things like DataContext fit into the picture.

Please educate me on these topics.

Samantha
  • 975
  • 6
  • 20
  • 1
    Start reading here: [Data Binding Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview). In short, your Window's DataContext would reference on object with a public property, that you would bind to. Whenever the value of the property changes, fire the PropertyChanged event. – Clemens May 11 '18 at 12:55
  • You have a couple of choices, you could just have the XAML and the UI code done in C#, and just reference your F# lib as the model. Or, take a look at [Gjallarhorn](https://github.com/ReedCopsey/Gjallarhorn) which will let you do both the UI and logic in F#. Gjallarhorn implements `mutables` and `signals` which you can bind to your XAML. In your example your stringfunction would be mapped over by a Signal for example. So whenever it change your UI would be updated. – s952163 May 11 '18 at 14:36
  • You use a DispatcherTimer to run StringFunction and update the value of a property on your ViewModel which is bound to a property in the UI. You'll need to implement the property such as to fire off the PropertyChanged event when updated. As to there not being any example, I was able to find this with a simple search https://stackoverflow.com/questions/1698147/f-using-inotifypropertychanged-for-data-binding you might want to look a little bit harder and create small prototypes to explore code before asking a question. It's harder, but it's better for you in the long run. –  May 11 '18 at 16:26
  • 1
    Gjallarhorn can also be used with the XAML in a C# WPF User Control Library (or VB I guess). I've found this to be better to avoid issues. – Bent Tranberg May 11 '18 at 19:50
  • 1
    @Will Thanks, I saw that question before posting. What Scott Hutchinson said there is precisely the point here. The implementation is available - but as stated in my question, there are no examples of how to *use it*. – Samantha May 12 '18 at 01:05
  • Yes, you are right, as UI development in general moved to the web, so you will find more Fable/elmish/React examples. However take a look at http://reedcopsey.com/2017/12/14/christmas-trees-in-wpf-2017-update/#more-398 or the gjallarhorn repo itself. – s952163 May 12 '18 at 09:46
  • I guess it's unclear what you mean by "use". The implementation is linked. You bind the property in the UI. You use a timer to run your StringFunction which updates the property. That's it. I could write a very simple example, but it'll be in C#, as I don't know F#. –  May 14 '18 at 16:24

0 Answers0