private UIElement CreateBody(Todos todos) { // Disconnect the ScrollViewer from it's previous ContentControl parent if (bodyContainer != null) { bodyContainer.Content = null; } bodyScroller.Content = new StackPanel() { Orientation = Orientation.Vertical } .Containing(todos.TodoItems.Select(CreateTodoItem)); return(bodyContainer = new ContentControl() { Content = bodyScroller }); }
private UIElement CreateHeader(Todos todos) { return(new Grid() { } .DoWith(g => { g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); g.ColumnDefinitions.Add(new ColumnDefinition()); }) .Containing( new TextBlock() { Text = "Add a todo", HorizontalAlignment = HorizontalAlignment.Stretch } .DoWith(t => t.SetValue(Grid.ColumnProperty, 0)), new TextBox() { }.DoWith(t => { t.Loaded += (sender, args) => t.Focus(); t.KeyDown += (sender, args) => { if (args.Key == Key.Enter) { args.Handled = true; Dispatch(TodoMessage.Add(new TodoItem(t.Text, false))); } }; }) .DoWith(t => t.SetValue(Grid.ColumnProperty, 1)) )); }
public MainWindow() { InitializeComponent(); component = Component.Create <Todos, TodoMessage, UIElement>( new Todos(ImmutableList <TodoItem> .Empty, false), (msg, todos) => msg.Map( toggleFilter: filter => new Todos(todos.TodoItems, filter), add: todo => new Todos(todos.TodoItems.Add(todo), todos.Filter), remove: todo => new Todos(todos.TodoItems.Remove(todo), todos.Filter), toggleCompleted: todo => new Todos( todos.TodoItems.Replace(todo, new TodoItem(todo.Text, !todo.Completed)), todos.Filter)), (todos) => new Grid() { Margin = new Thickness(5, 5, 5, 5) } .WithRow(Height: GridLength.Auto) .WithRow() .WithRow(Height: GridLength.Auto) .Containing( CreateHeader(todos).DoWith(t => t.SetValue(Grid.RowProperty, 0)), CreateBody(todos).DoWith(s => s.SetValue(Grid.RowProperty, 1)), new TextBlock() { Text = "Show Completed" }.DoWith(t => t.SetValue(Grid.RowProperty, 2))) ); this.model = component.InitialModel; this.Content = component.View(model); }
private void Dispatch(TodoMessage msg) { model = component.Update(msg, model); this.Content = component.View(model); }