I have a ContentControl whose content is being set in a ViewModel through binding. I have a couple things that I would like to programatically set on the view/template that is being applied to the data object. If I understand it right, the "Template" property is for the ContentControl, not the actual Content of the ContentControl. How would I access the actual view obejct when WPF creates it and applies it? For the example below, I want to make an adjustment to the vw:InfoType1View or vw:InfoType2View object when it gets instantiated.
<ContentControl Name="mainContentArea" Content="{Binding CurrentInfo}">
<ContentControl.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:InfoType1}">
开发者_JAVA百科 <vw:InfoType1View />
</DataTemplate>
</ResourceDictionary>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:InfoType2}">
<vw:InfoType2View />
</DataTemplate>
</ResourceDictionary>
</ContentControl.Resources>
</ContentControl>
I am assuming you are using the MVVM pattern? In this case, you really shouldn't be programmatically making changes to your view!
Anyhow, you could handle the Loaded event, or the LayoutUpdated event (hard to determine which you need without more code). You can then navigate the visual tree, using my Linq-to-VisualTree for example, to navigate the elements in the view that was constructed - and make your changes.
精彩评论