views:

73

answers:

1

I am relatively new to WPF, so I apologize if I am missing something basic.

I have a user control where I declare dependency properties named PT1x, PT1y, PT2x, PT2y:

   Private _pt1x as double = 9

   Public Property PT1x As Double
        Get
            Return GetValue(PT1xProperty)
        End Get
        Private Set(ByVal value As Double)
            SetValue(PT1xProperty, value)
        End Set
    End Property

    Public Shared ReadOnly PT1xProperty As DependencyProperty = _
                           DependencyProperty.Register("PT1x", _
                           GetType(Double), GetType(Tile), _
                           New FrameworkPropertyMetadata(_pt1x))

etc...

I set the datacontext of the usercontrol in xaml:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

And then bind to the property in xaml:

    <Line 
        X1="{Binding PT1x}" Y1="{Binding PT1y}"
        X2="{Binding PT2x}" Y2="{Binding PT2y}"
        Stroke="Red"
        StrokeThickness="1"
        x:Name="HS2" />

This renders a line at runtime, but at design time there is nothing rendered in the designer, in either blend or vs 2010. Is there a way to have it render in the designer?

Thanks!

A: 

That's because Blend annoyingly does not execute any code defined by your outer-most control (i.e. your UserControl). It instead provides its own host in which your visual tree resides (minus your outermost control)^. Therefore, the DataContext is never set. I'd argue that it's not good practice to set the DataContext at the UserControl level anyway, because anyone can switch it on you:

<local:YourUserControl DataContext="Haha! All your DC are belong to us!"/>

You're better off with something like this:

<UserControl x:Name="root">
    <Grid DataContext="{Binding ElementName=root}">
    </Grid>
</UserControl>

HTH,
Kent

^ Yes, this causes issues other than the one you're experiencing. Yes, it is incredibly painful. No, I don't know why they don't just host your full control in their host.

Kent Boogaart
I changed my code as you suggested, but this does not render anything in the designer, either (VS2010 -or- Blend). Just curious: what other issues might doing it this way cause?
Gerald