Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`<Run />` doesn't databind in designtime

I have a view model that inherits from ReactiveObject from reactiveui.net, something like

public sealed class TestViewModel : ReactiveObject
{
    public sealed class NestedViewModel
    {
        private string _property;
        public string VMProperty
        {
            get { return _property; }
            set { this.RaiseAndSetIfChanged(ref _property, value); }
        }

        private string _suffix;
        public string Suffic
        {
            get { return _suffix; }
            set { this.RaiseAndSetIfChanged(ref _suffix, value); }
        }

    }

    private NestedViewModel _nested = new NestedViewModel();
    public Nested
    {
        get { return _nested; }¨
        set { this.RaiseAndSetIfChanged(ref _nested, value); }
    }

#if DEBUG
    public TestViewModel() {
        Nested.VMProperty = "Test string";
        Nested.Suffix = "with suffix";
    }
#endif
}

I can get the following to display both design-time and run-time:

 <Page.DataContext>
     <local:TestViewModel />
 </Page.DataContext>

 <TextBlock Text="{Binding Nested.VMProperty}" />
 <TextBlock Text="{Binding Nested.Suffix}" />

but when I try to do this instead, no text is displayed design-time:

 <Page.DataContext><!-- ... -->

 <TextBlock>
     <Run Text="{Binding Nested.VMProperty}" />
     <Run Text="{Binding Nested.Suffix}" />
 </TextBlock>

Run-time it still works, but I don't want to have to deploy to the device emulator every time I want to check some pixel pushing...

How do I get these properties do display inside a <Run /> tag during design time?

like image 360
Tomas Aschan Avatar asked May 18 '15 10:05

Tomas Aschan


2 Answers

Paul Betts, the creator of the ReactiveUI framework, advocates hard-coding sample data into the Page's XAML:

<TextBlock>
 <Run Text="Test String" x:Name="VMproperty" />
 <Run Text="with suffix" x:Name="Suffix" />
</TextBlock>

You can then do the binding in the Page's code behind:

this.OneWayBind(ViewModel, vm => vm.VMproperty, v => v.VMproperty.Text);
this.OneWayBind(ViewModel, vm => vm.Suffix, v => v.Suffix.Text);

These ReactiveUI style bindings overwrite the sample data that was hard coded in the XAML. So you get sample data at design-time and data binding at runtime.

Source: https://groups.google.com/d/msg/reactivexaml/GrVHWm8tUuM/4EAxOsxc_LQJ

like image 62
Thomas Avatar answered Oct 29 '22 16:10

Thomas


What about using FallbackValue in your binding? I use it frequently to check how bound content would look like and didn't have any problem.

like image 43
Filip Avatar answered Oct 29 '22 17:10

Filip