10

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?

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • It is probably not what you hope for, but a similar case has been discussed and answered for WP7 [here](http://stackoverflow.com/q/5253622/650012) – Anders Gustafsson May 22 '15 at 09:09
  • @AndersGustafsson Thanks for the tip, although it doesn't solve the problem for me. – Tomas Aschan May 22 '15 at 09:36
  • I've played a little with *DesighData*, and it seems that *TextBlock* with *Run* method won't work like you have mentioned in question. The other thing is that this combination (TB with Run) will successfully work in design mode when used in *DataTemplate* - you can take a look at [my sample](http://1drv.ms/1EqduWg) - see MainPage.xaml. – Romasz May 23 '15 at 23:28
  • Where are you doing the linkage between the view and the viewmodel. the view suppose to use ViewFor... if you do so the usage of xaml is obsolete. your thoughts? – DeJaVo May 26 '15 at 20:08
  • @DeJaVo: I do make the page implement `IViewFor`, but I can't have it inherit `ViewFor` since also need to have my own base class, and I haven't been able to make it work with a generic base class. Also, because of how the VM layer is implemented (by someone else, i.e. out of my control), I must sometimes assign the viewmodel in the `OnNavigatedTo` event handler. – Tomas Aschan May 27 '15 at 06:28

2 Answers2

2

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

Thomas
  • 665
  • 1
  • 6
  • 11
1

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.

Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35
Filip
  • 1,824
  • 4
  • 18
  • 37
  • It's a great idea, but `FallbackValue` doesn't render anything design-time either... :( – Tomas Aschan May 22 '15 at 11:26
  • Did some more search and unfortunately just does not work in design-time. I'd simply create another property on the VM that will combine VMProperty and Suffix to single string.. – Filip May 22 '15 at 11:37
  • Unfortunately, in this project I'm not in power of the VM layer - they are shared across several platforms using Xamarin. But since there doesn't seem to be any other options, I'll see if I can get this fixed. – Tomas Aschan May 26 '15 at 09:30