2

FSarp.ViewModule我有一个简单的视图中,单击按钮会根据右侧的文本框在左侧的文本框中生成一个随机数。我将如何绑定右侧文本框中的更改(例如手动输入数字),以便通过调用 clickcommand 或 click() 方法而不单击按钮来更新左侧文本框?我尝试了不同的组合,self.DependencyTrackerself.RaisePropertyChanged无法正确使用语法。

这是视图:

看法

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})
4

1 回答 1

2

您只需要click在 Data2 的 setter 中调用该函数:

member self.Data2 with get() = data2.Value 
                  and set v = data2.Value <- v ; click()
于 2016-05-31T16:07:32.343 回答