我有一个ContentControl,其内容通过绑定在ViewModel中设置。我有几件事我想以编程方式设置在应用于数据对象的视图/模板上。如果我理解正确,“Template”属性用于ContentControl,而不是ContentControl的实际内容。当WPF创建并应用它时,我如何访问实际的视图obejct?对于下面的示例,我想在实例化时对vw:InfoType1View或vw:InfoType2View对象进行调整。
<ContentControl Name="mainContentArea" Content="{Binding CurrentInfo}">
<ContentControl.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:InfoType1}">
<vw:InfoType1View />
</DataTemplate>
</ResourceDictionary>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:InfoType2}">
<vw:InfoType2View />
</DataTemplate>
</ResourceDictionary>
</ContentControl.Resources>
</ContentControl>
答案 0 :(得分:0)
我假设您正在使用MVVM模式?在这种情况下,您真的不应该以编程方式对视图进行更改!
无论如何,您可以处理Loaded事件或LayoutUpdated事件(如果没有更多代码,很难确定您需要哪些事件)。然后,您可以使用我的Linq-to-VisualTree导航可视树,以导航构建的视图中的元素 - 并进行更改。