public void Should_Bind_To_Element() { TextBlock source; ContentControl target; var root = new TestRoot { Child = new StackPanel { Children = new Controls.Controls { (source = new TextBlock { Name = "source", Text = "foo", }), (target = new ContentControl { Name = "target", }) } } }; var binding = new Binding { ElementName = "source", }; target.Bind(ContentControl.ContentProperty, binding); Assert.Same(source, target.Content); }
public Form Panel <T>(string modelFieldName, Action <Form> populatePanel) { /* * Here is documentation on how ContentControl works: * https://docs.avaloniaui.net/docs/controls/contentcontrol */ var panelControl = new Avalonia.Controls.ContentControl(); if (!(getModelValue(modelFieldName) is T)) { throw new Exception( $"Model {nameof(modelFieldName)} source property specified by name [{modelFieldName}] is not of type T: {typeof(T).Name}"); } /* * One Way binding, because the ContentControl cannot set the model back, it just displays the model */ AddBinding <object>(modelFieldName: modelFieldName, control: panelControl, property: Avalonia.Controls.ContentControl.ContentProperty, isTwoWayDataBinding: false); panelControl.ContentTemplate = new Avalonia.Controls.Templates.FuncDataTemplate <T>((itemModel, nameScope) => { // if the model is null then just display nothing if (itemModel == null) { // how could itemModel be null? Sometimes it is though, so strange var tb = new Avalonia.Controls.TextBlock(); tb.Text = "(null)"; return(tb); } var contentForm = new Form(__app: this.app, _model: new lib.BindableDynamicDictionary()); contentForm.DataContext = itemModel; populatePanel(contentForm); contentForm.Host.DataContext = itemModel; return(contentForm.Host); }); AddRowToHost(panelControl); return(this); }
internal ContentPresenter(ContentControl templatedParent) { this.Content = templatedParent.Content; }
public void AvaloniaObject_this_Operator_Accepts_Binding() { var target = new ContentControl { DataContext = new { Foo = "foo" } }; target[!ContentControl.ContentProperty] = new Binding("Foo"); Assert.Equal("foo", target.Content); }
public void Should_Bind_To_Later_Added_Element() { ContentControl target; StackPanel stackPanel; var root = new TestRoot { Child = stackPanel = new StackPanel { Children = new Controls.Controls { (target = new ContentControl { Name = "target", }), } } }; var binding = new Binding { ElementName = "source", }; target.Bind(ContentControl.ContentProperty, binding); var source = new TextBlock { Name = "source", Text = "foo", }; stackPanel.Children.Add(source); Assert.Same(source, target.Content); }