ReactivePropertyって別にC#専用ってわけじゃないんだからねっ!!
// 名前空間のopenは省略・・・ // ViewModelの定義はスッキリ type SampleViewModel() = let name = new ReactiveProperty<string>() let upper = name.Select(fun (s : string) -> s.ToUpper()) .Delay(TimeSpan.FromSeconds(2.0)) .ToReactiveProperty() member x.Name = name member x.Upper = upper // windowのcontentの組み立て let grid = Grid() grid.RowDefinitions.Add <| RowDefinition(Height = GridLength.Auto) grid.RowDefinitions.Add <| RowDefinition(Height = GridLength.Auto) let textBox1 = TextBox() let textBox2 = TextBox() textBox1.SetBinding(TextBox.TextProperty, new Binding("Name.Value", Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged)) |> ignore textBox2.SetBinding(TextBox.TextProperty, new Binding("Upper.Value", Mode = BindingMode.TwoWay)) |> ignore Grid.SetRow(textBox1, 0) Grid.SetRow(textBox2, 1) grid.Children.Add textBox1 grid.Children.Add textBox2 // windowそ作成 let w = Window( Title = "Hello RxProperty F#", Width = 500.0, Height = 400.0, DataContext = SampleViewModel(), Content = grid) // GO! Application().Run(w)
画面は、普通XAMLで組み立てるものを無理やりF#で組み立ててるのでごちゃごちゃしてますがViewModelの記述じたいはすっきりしていいかも。