如何在FSharp.ViewModule中将命令/属性绑定到PropertyChanged

时间:2016-05-31 10:05:30

标签: wpf xaml mvvm f# fsharp.viewmodule

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

这是观点:

View

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"/>

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

1 个答案:

答案 0 :(得分:2)

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

member self.Data2 with get() = data2.Value 
                  and set v = data2.Value <- v ; click()