So I have some code similar to the following: (Forgive any typos-- I tried to simplify in the SO editor for the post)
<my:CustomContentControl>
<my:CustomContentControl.Style>
<Style TargetType="{x:Type my:CustomContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentView}" Value="MyCustomView">
<Setter Property="Content">
<Setter.Value>
<my:CustomView DataContext="{Binding DataContextForMyCustomView"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</m:CustomContentControl.Style>
</my:CustomContentControl>
The problem is that whenever the DataTrigger
occurs, the setter does set the Content
property to my:CustomView
, but it does not bind DataContext
. If I move the same code outside of the trigger the DataContext
binding works just fine.
Any ideas? If this is a limitation of some sorts, is there any work around?
Update:
I received the following error in the output window:
System.Windows.Data Error: 3 : Cannot find element that provides DataContext. BindingExpression:Path=DataContextForMyCustomView; DataItem=null; target element is 'CustomView' (Name='customView'); target property is 'DataContext' (type 'Object')
I solved a similar problem by putting the UserControl into the resources and then changing the Content with that.
e.g. from my own code (different names, same concept)
The error you posted makes it sound like your custom control is in an object that doesn't have a
DataContext
, such as aDataGridColumn.Header
.To get around that, you can create a Freezeable object in your
.Resources
containing the binding you're looking for, then bind yourmy:CustomView.DataContext
to that objectHere's the code for a sample
Freezable
object copied from here:Also, you really should use
ContentTemplate
instead ofContent
to avoid an exception if more than one object applies that style :)