xaml design-time data

65 views
Skip to first unread message

Matt Oswald

unread,
Mar 25, 2014, 12:24:01 PM3/25/14
to reacti...@googlegroups.com
I expected that putting this in my View's constructor

if (ModeDetector.InDesignMode())
{
   City.Text = "Austin";
}

would display "Austin" in either Blend or the Visual Studio design window. ...Why doesn't it?

--m

Paul Betts

unread,
Mar 25, 2014, 12:38:06 PM3/25/14
to ReactiveUI mailing list
Hey Matt,

Design-time Mode is a Magical Mystical place, where certain code runs and certain code doesn't; it's completely undocumented. Where you put that code will decide whether it runs.

However, in RxUI if you're using ReactiveUI bindings, there's a far simpler way to do design-time data than what Microsoft recommends. It's going to sound crazy and feel Weird, but humor me. 

<!-- Just write the sample data in -->
<TextBlock x:Name="City" Text="Austin" />

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

You can just type in the sample data directly. No magic data. No creating bizarre fakes in Xaml. 

It even works for ItemsControls, because if a control has both Items and ItemsSource, the latter trumps the former. So, basically all you do is drag-drop instances of your UserControls onto the ListBox and you'll end up with this:

<ListBox x:Name="MyList">
<ListBox.Items>

<!-- Because you've done the same sample data for this control too, you'll see
     the listbox populated with data -->
<ctl:MyListItemUserControl />
<ctl:MyListItemUserControl />
<ctl:MyListItemUserControl />

</ListBox.Items>
</ListBox>

// ItemsSource trumps Items, so none of the dummy user controls will show
this.OneWayBind(ViewModel, vm => vm.Items, v => v.MyList.ItemsSource);

Paul



--
You received this message because you are subscribed to the Google Groups "ReactiveUI mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivexaml...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages