2

In FSarp.ViewModule I have a simple view where clicking the button generates a random number in the left textbox, based on the right textbox. How would I bind the a change in the right textbox (e.g. inputting a number by hand) so that it would update the left textbox by invoking the clickcommand or click() method without clicking the button? I tried different combinations of self.DependencyTracker and self.RaisePropertyChanged but couldn't get the syntax right.

This is the view:

View

The XAML:

<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="55" Margin="130,55,0,0" VerticalAlignment="Top" Width="115" Command="{Binding Click}"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="50" Margin="30,55,0,0" TextWrapping="Wrap" Text="{Binding Data, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="70"/>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="50" Margin="280,55,0,0" TextWrapping="Wrap" Text="{Binding Data2, UpdateSourceTrigger=PropertyChanged}"  VerticalAlignment="Top" Width="70"/>

And the ViewModel:

type RandomX = {
    DataOut: int
    DataIn: int
    }

type MainRandomModel(model:RandomX) as self = 
    inherit ViewModelBase()
    let rnd = System.Random()

    let defaultNums = model //{DataOut = 100; DataIn = 100}

    let data = self.Factory.Backing(<@ self.Data @>, defaultNums.DataOut )
    let data2 = self.Factory.Backing(<@ self.Data2 @>, defaultNums.DataIn)
    let click() = data.Value <- rnd.Next(data2.Value) 
    let clickcommand = self.Factory.CommandSync(click)
    member self.Click = clickcommand
    member self.Data with get() = data.Value and set v = data.Value <- v
    member self.Data2 with get() = data2.Value  and set v = data2.Value <-v

    new() = MainRandomModel({DataOut=20;DataIn=10})
FoggyFinder
  • 2,230
  • 2
  • 20
  • 34
s952163
  • 6,276
  • 4
  • 23
  • 47

1 Answers1

2

You just need to call the click function in the setter for Data2:

member self.Data2 with get() = data2.Value 
                  and set v = data2.Value <- v ; click()
FoggyFinder
  • 2,230
  • 2
  • 20
  • 34