8

I am building a UserControl that has several DependencyProperties that I use for the bindings of the XAML of the control.

When I work with MVVM I normally create a design time ViewModel, because I find that way it is easier to setup the layout of my Views without having to run the application.

Is there a way to set design time data to my dependency properties in an UserControl?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Dzyann
  • 5,062
  • 11
  • 63
  • 95
  • 1
    Don't you ultimately bind the UserControl's DPs to a view-model anyway? – McGarnagle Feb 21 '14 at 19:02
  • 3
    @McGarnagle I am creating a control for someone in another project to use. They will have the ViewModel. I have a "test" project where I will create a TestViewModel to use my control, but I would like my control be "self-contained". So I would like to be able to set design data at the control level. – Dzyann Feb 21 '14 at 19:05

3 Answers3

1

In yourxaml, define this

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DataContext="{d:DesignInstance prefix:ViewModel}"

That should give you the properties in the ViewModel..

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • 1
    Using the DataContext works as long as the Binding properties are there. But in a UserControl you are binding to the Properties of the Control, not the DataContext. Is there a way to overcome that? – Dzyann Feb 21 '14 at 20:17
0

I suggest setting the DataContext of your control to the TestViewModel instance. You can ensure this is done in design-mode only by using DesignerProperties.IsInDesignMode (DesignerProperties.IsInDesignTool for Silverlight):

public MyUserControl()
{
    if (DesignerProperties.IsInDesignMode)
        DataContext = new TestViewModel();
}
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • @Dzyann no prob -- I guess "IsInDesignMode" is the one I meant. My mistake, I'm used to working in Silverlight. – McGarnagle Feb 21 '14 at 19:33
  • I hadn't notice that was a Silverlight Property. However IsInDesignMode it is not presente either (maybe can't be accessed from code-behind?). I tried using the DesignerProperties.GetIsInDesignMode(this) and even (bool)GetValue(DesignerProperties.IsInDesignModeProperty) but it doesn't work. The designer doesn't show anything. – Dzyann Feb 21 '14 at 19:44
-1

Here's how I achieve the goal. This does not work if you want to use an inherited data context in addition to the dependency properties.

  1. Bind the control's data context to the control itself:

    <UserControl ...
                 DataContext="{Binding RelativeSource={RelativeSource Self}}">
    
  2. Use "normal" bindings in the control:

    <UserControl ...>
        <TextBlock Text="{Binding MyDependencyProperty}"/>
    
  3. Add a design-time data context whose properties have the same names as the control's dependency properties:

    <UserControl ...
                 DataContext="{Binding RelativeSource={RelativeSource Self}}"
                 d:DataContext="{d:DesignInstance local:DesignTimeViewModel, IsDesignTimeCreatable=True}">
    
    class DesignTimeViewModel
    {
        public string MyDependencyProperty { get; } = "The design-time value";
    }
    

At runtime it will bind to the dependency properties, and at design-time it will bind to the DesignTimeViewModel properties.

Vimes
  • 10,577
  • 17
  • 66
  • 86