3

Hi I have a simple user control with some Blend generated design time data:

<UserControl x:Class="CustomerProfile.View.CustomerProfile"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:vm="clr-namespace:CustomerProfile.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="400" d:DesignWidth="400">

<Grid Margin="8" d:DataContext="{d:DesignData /SampleData/CustomerProfileViewModelSampleData.xaml}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Label Content="Surname" />
    <TextBox Grid.Column="1" Text="{Binding Account.Surname}" />

</Grid>

This is all good, and when I open the user control in Blend or Cider I see the generated data.

However, I am using the UserControl in a Window, like this:

<Window x:Class="Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cp="clr-namespace:CustomerProfile.View;assembly=CustomerProfile"
    Title="Demo" Height="350" Width="525">
<Grid>
    <cp:CustomerProfile />
</Grid>

When I open this Window in Blend or Cider I do NOT see the design time data in the User Control.

Is there any way around this? Am I going about things the wrong way?

Thanks in advance...

Paul
  • 815
  • 1
  • 13
  • 23
  • Does it make a difference if you move your `d:DataContext` to the `` tag instead of the ``? Right now your design-time DataContext is inside your UserControl, instead of being part of it. – Rachel Feb 01 '12 at 17:29
  • Thanks for the reply...I tried that but exactly the same result. – Paul Feb 01 '12 at 17:59
  • what happens if you set `` – Jake Berger Feb 01 '12 at 18:21
  • i think it may be because the Window's child controls "inherit" it's DataContext (which is nothing). also, the designer takes the root control (`Window` when `Window` is being designed) into special consideration to others in the designer (root control's ctor does NOT execute). – Jake Berger Feb 01 '12 at 18:28
  • Thanks @jberger. I had a slight problem in setting because my UserControl is in a seperate project to the one with the main Window in. And that seperate project is the one with the design time data! BUT...just in the interest of trying it out I created a Window in the same project as my UserControl and design time data, and set the d:DataContext on that window, and yes it did display the data in the UserControl within the window. Not sure where that leaves me really, I need the UserControl in a seperate project as it's a reusable control! – Paul Feb 02 '12 at 10:10
  • @jberger I found you can navigate through the folders in the reference to d:DataContext! So I manually went in and set the Window design time DataContext to reference the data from the other project using "../" to move up to the parent folder. Like this: `` worked a treat. Thanks for the help! (note that I had to do it manually in XAML I could not do it with drag & drop in Blend) – Paul Feb 02 '12 at 10:20
  • @jberger I spoke too soon, the solution no longer builds when I try and pull the design data in from the other project!! – Paul Feb 02 '12 at 10:25
  • @Paul: you can add the design data file **as a link** to your Window's project. That way, when you edit the file in one project, it gets reflected to the other.. – Jake Berger Feb 02 '12 at 15:00
  • @jberger: I liked the idea, sounded like it would work. However the data does not display when the design data is added as a link to the Window's project. The compiler underlines the `d:DesignData...` with a _file not found_ error. I did set the Build Action of the linked file to "DesignData". – Paul Feb 06 '12 at 10:57
  • what happens if you: set your ` – Jake Berger Feb 06 '12 at 14:51
  • i'd suggest just doing that. i don't consider sample data to be part of DRY, because it's really a designer preference. – Jake Berger Feb 06 '12 at 19:34
  • @jberger : yeah I will live with it. Many thanks..! – Paul Feb 06 '12 at 23:35
  • I think you should cut the relevant bits from comments and form an answer, and accept it ;) – quetzalcoatl Apr 15 '14 at 09:09

0 Answers0