3
在FSarp.ViewModule
我有一個簡單的視圖,點擊該按鈕會在左邊的文本框中根據正確的文本框生成一個隨機數。我將如何綁定右側文本框中的更改(例如,手動輸入數字),以便在不單擊按鈕的情況下通過調用clickcommand或click()方法來更新左側文本框?我嘗試了self.DependencyTracker
和self.RaisePropertyChanged
的不同組合,但無法獲得正確的語法。我如何將一個命令/屬性綁定到FSharp.ViewModule中的PropertyChanged
這是視圖:
的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"/>
和視圖模型:
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})