开发者

How Do I Get the Instantiated DataTemplate object for an item displayed in a ContentControl?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜