How to bring ViewModel reference to View (in ViewModel First)

Sep 2, 2010 at 4:53 PM

I have very special needs for databinding.

I have working code in plain SilverLight without Caliburn, but dont know how to rewrite in Caliburn.

Working code without Caliburn (simple View First) :

  <my:MyViewModel x:Key="MyViewModel"/>  
 <telerik:RadGridView ... >
    <telerik:GridViewDataColumn  IsVisible="{Binding IsColumnVisible,  Source={StaticResource MyViewModel}}" />

The point is that GridViewDataColumn has not DataContext .. so Source={} is needed.

How to reference Caliburn (or MEFs) ModelView ?

Sep 3, 2010 at 12:47 AM

I can think of two possible options:

1. Does the <telerik:GridViewDataColumn ...> have a content property so you can do <telerik:GridViewDataColumn><ContentControl x:Name="MyViewModel" /> </telerik:GridViewDataColumn>

2. Or you can do what I did with the Actipro controls, and modify the FindNamedElements functor to search through the Actipro custom controls. However, you will need to make sure that elements up the stack are named (<telerik:RadGridView>). If they are not named, you should try that first.

If option 1 doesnt work, let me know. I can try and post a sample of my custom control blog post this weekend.


Sep 3, 2010 at 8:41 AM
Edited Sep 3, 2010 at 8:43 AM

You can try something like this:


	<FrameworkElement x:Key="bridge" />
	<Binding Mode="OneWayToSource" Path="DataContext" Source="{StaticResource bridge}" />


<telerik:RadGridView ... >
    <telerik:GridViewDataColumn IsVisible="{Binding DataContext.IsColumnVisible, Source="{StaticResource bridge}" />


The first block "captures" UserControl's DataContext (which, in turn, is bound to your VM if you used Caliburn's automatic view lookup & model-first) into a named element; the binding leverages access to the "bridge" element by name from resources.
Is an embarrassingly ugly trick, but it should overcome GridViewDataColumn limitation (btw, I can't believe they did something that awkward...)


Sep 4, 2010 at 10:03 PM
Edited Sep 4, 2010 at 11:31 PM

Hello guys,
thanks to both.

For dbuksbaum :
first point dont work .. GridViewDataColumn has not content property.
second probably work .. but seems to me to hacky for simple application .. for big LOB i probably go this way

For marcoamendola:
this dont work for silverlight (silverlight has not OneWayToSource)
I rewrite this statemen for Silverlight

        <Binding Mode="TwoWay" Path="DataContext" Source="{StaticResource bridge}" UpdateSourceTrigger="Explicit"/>

but this still do not work.

Anyway I discover another simply way.

Manually put ViewModel into View resource (must be before InitializeComponent ).

        public ShellView()
            Resources.Add("ViewModel", IoC.Get<ShellViewModel>());


and reference that resource in xaml

<Controls:GridViewDataColumn x:Name="ColumnCodeModul" IsVisible="{Binding Path=IsCodeVisible, Source={StaticResource ViewModel}}"/>