Get Usercontrol datacontext via element binding

Mar 11, 2011 at 6:42 PM

I've got a button in a data template that is bound to a property within my view model. Meaning that the data context of the button is to property on the view model and not the view model   I'd like to bind the click command to a cinch simple command in my view model. The view model is attached to the user control with MEFedMVVM.  

My question is, when MEFedMVVM attaches the view model... can element binding be used?  Been trying to get this working for sometime so I thought I'd throw a post out there.

<UserControl
	...
	mefed:ViewModelLocator.ViewModel="UserViewModel"             	
        x:Name="UserManagementControl">

 

In order do element binding I provided a name for my usercontrol as can been seen in the code above..

 

 

<Button Grid.Column="1" Height="50" Content="Add to Assigned Roles">
	<i:Interaction.Triggers>
		<i:EventTrigger EventName="Click">
                         <CinchV2:EventToCommandTrigger Command="{Binding ElementName=UserManagementControl, Path=DataContext.AddUserRoleCommand}"/>                                                    
		</i:EventTrigger>
	</i:Interaction.Triggers>	
</Button>

 

 

Coordinator
Mar 11, 2011 at 7:18 PM
a datatemplate has its own Namescope thus you cannot use ElementBinding. Maybe you should see if you can do it via RelativeSource binding (if this is WPF) and walk the try to get the datacontext you want

{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.AddUserRoleCommand}

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Fri, Mar 11, 2011 at 11:42 PM, acanadian <notifications@codeplex.com> wrote:

From: acanadian

I've got a button in a data template that is bound to a property within my view model. Meaning that the data context of the button is to property on the view model and not the view model I'd like to bind the click command to a cinch simple command in my view model. The view model is attached to the user control with MEFedMVVM.

My question is, when MEFedMVVM attaches the view model... can element binding be used? Been trying to get this working for sometime so I thought I'd throw a post out there.

<UserControl
	...
	mefed:ViewModelLocator.ViewModel="UserViewModel"             	
        x:Name="UserManagementControl">

In order do element binding I provided a name for my usercontrol as can been seen in the code above..

<Button Grid.Column="1" Height="50" Content="Add to Assigned Roles">
	
		"Click">
                         <CinchV2:EventToCommandTrigger Command="{Binding ElementName=UserManagementControl, Path=DataContext.AddUserRoleCommand}"/>                                                    
		
		
</Button>

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Mar 12, 2011 at 3:01 AM

Thats right I forgot about that.  I am working in Silverlight V4 so I did some digging to find a way forward.  Found this site that allowed to wire things up nicely. Actually a pretty slick solution to the missing functionality in SilverLight.

http://blog.thekieners.com/2010/09/08/relativesource-binding-with-findancestor-mode-in-silverlight/

 

<Button Grid.Column="1" Height="50" Content="Add to Assigned Roles">
    <helpers:BindingHelper.Binding>
        <helpers:RelativeSourceBinding Path="DataContext.AddUserRoleCommand" 
               TargetProperty="Command" 
               RelativeMode="FindAncestor"
               AncestorType="UserControl" />
    </helpers:BindingHelper.Binding>
</Button>