`<Run />` doesn't databind in designtime
Asked Answered
I

2

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?

Idealist answered 18/5, 2015 at 10:38 Comment(5)
It is probably not what you hope for, but a similar case has been discussed and answered for WP7 hereRadiothermy
@AndersGustafsson Thanks for the tip, although it doesn't solve the problem for me.Idealist
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 - see MainPage.xaml.Diencephalon
Where are you doing the linkage between the view and the viewmodel. the view suppose to use ViewFor<T>... if you do so the usage of xaml is obsolete. your thoughts?Pegg
@DeJaVo: I do make the page implement IViewFor<T>, but I can't have it inherit ViewFor<T> 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.Idealist
W
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

Waylonwayman answered 27/5, 2015 at 9:2 Comment(0)
U
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.

Unsought answered 22/5, 2015 at 10:44 Comment(3)
It's a great idea, but FallbackValue doesn't render anything design-time either... :(Idealist
Did some more search and unfortunately <Run /> 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..Unsought
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.Idealist

© 2022 - 2024 — McMap. All rights reserved.