Using XAML UserControl from Infrastructure in another module...

Topics: Prism v4 - WPF 4
Aug 27, 2012 at 5:36 PM
Edited Aug 27, 2012 at 5:37 PM

Hello, 

I have a user control (common XMAL code with some binding syntax in it), I want the location where I place the control to be used for a datasource/datacontext. 

I defined my xaml in infrastructure such as the following: 

<Grid>
        <Popup x:Name="openViewsPopup"
               IsOpen="{Binding ElementName=openViewsToggle, Path=IsChecked}"
               StaysOpen="False"
               Cursor="Hand"
               MaxHeight="350"
               ForceCursor="True"
               PlacementTarget="{Binding ElementName=openViewsToggle}"
               Placement="Top">
            <ListBox ItemsSource="{Binding OpenViews}"
                 SelectedValuePath="UriString"
                 SelectionMode="Single"
                 MinWidth="100" MinHeight="100"
                 MouseDoubleClick="{Binding OpenViewCommand}" />
        </Popup>

</Grid>

 I am adding this to another control such as the following: 

 <common:OpenViewsControl />

the viewmodel for the view in the module where I am placing OpenViewsControl, has all the properties needed to populate the user control, but when I am running this I am running into a binding error. 

I am setting the ViewModel in Code behind as follows: 

[Dependency]
		public MenuViewModel ViewModel
		{
			get { return DataContext as MenuViewModel; }
			set { DataContext = value; }
		}

But for whatever reason is not working.  Can anyone guide me on what I am doing wrong here?  I need to use the OpenViewsControl in many places. 

Thanks in advance for your assistance.


Developer
Aug 27, 2012 at 7:22 PM

Hi,

Based on the information you provided so far, it doesn't seems to be anything wrong about how you are setting the view model in the view. Therefore, the OpensViewsControl should inherit the DataContext of the view without problems.

However, take into account that for the properties of a class to be populated automatically, you will need to resolve that class through the container. Hence, if you are not resolving the view through the container, its ViewModel property is not being set by it and the OpensViewControl will not have a DataContext to inherit.

Also, if you are adding the OpensViewControl inside a DataTemplate in the view, the control wouldn't be able to reach the view's DataContext. This is because, by design, a DataTemplate has its own DataContext, which is the element to which the DataTemplate is applied to. If the OpensViewControl is added in a DataTemplate inside the view, it will inherit the DataTemplate's DataContext instead of the view's DataContext.

Finally, this problem could also be caused by how you are setting the source object for each binding. For example, in the code snippet provided above it can be seen a couple of bindings where its ElementName property is set to "openViewsToggle." This means that this binding will not use the DataContext as a source; it will look in the visual tree for a element named "openViewsToggle"and use it as the source instead. So, if no element with that name is found, the binding will fail. You can read more about this in the following article of MSDN:

As I mentioned before, with the information provided so far we are not able to find the cause behind this problem. Hence, it would be helpful if you could provide us with more information regarding this problem, like for example the binding error, how the view is added / constructed, etc, so that we can help you further with this.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

openViewsToggle