Reputation: 232
I'm having trouble setting up nested styles in WPF. I don't know if I'm doing things the 'right way' - but I'll describe what I'm attempting to do and provide some code.
I'm working with Kinect and am using a ContentControl to represent a Kinect object in my ViewModel. I declare it as such:
<ContentControl Content="{Binding Kinect}" ContentTemplate="{StaticResource SkeletonTemplate}" />
I then set up the SkeletonTemplate as such:
<DataTemplate x:Key="SkeletonTemplate">
<Grid>
<ContentControl Content="{Binding HandLeft}" ContentTemplate="{StaticResource JointTemplate}"/>
<ContentControl Content="{Binding HandRight}" ContentTemplate="{StaticResource JointTemplate}"/>
</Grid>
</DataTemplate>
Lastly, I set up a JointTemplate as such:
<DataTemplate x:Key="JointTemplate">
<Ellipse Fill="Red" Margin="0,0,620,460">
<Ellipse.RenderTransform>
<TranslateTransform X="{Binding Path=Position.X}" Y="{Binding Path=Position.Y}" />
</Ellipse.RenderTransform>
</Ellipse>
</DataTemplate>
Everything is all hunky dory except I'd really like to be able to set a Style at highest level that allows me to style the nested elements. For example, I want to be able to do this:
<ContentControl Content="{Binding Kinect}" Style="{DynamicResource ShrunkBlueSkeleton}" ContentTemplate="{StaticResource SkeletonTemplate}" />
And immediately apply a set of rules to the nested elements. Make the ellipses Blue, apply a ValueConverter to the ContentControls in the SkeletonTemplate to scale them to a smaller part of the screen, etc.
I'm having a bitch of a time getting it working, and I'm not sure if I've even set up everything up in the 'right' way to this point.
Certainly I could re-declare a ton of different ContentTemplates that style in all the different ways I need to, but that is much more obviously bad style.
Any pros out there that can lend a hand?
I could paste the Style attempts I've made thus far, but I'm rather convinced they won't help the discussion.
Upvotes: 1
Views: 223
Reputation: 10797
The root feature that you are missing is that Style's setters works on Properties and not on Path (a la Binding) - which I believe is what you would like to have.
There are technical reasons for Microsoft did that: Style works on specific styles and not on compound elements. Similar styling frameworks (most notably: CSS) also support "setters" just for one element (although, arguably, the select is much superior).
Upvotes: 1