我有一个WPF应用程序,主要遵循MVVM,我试图自动化。ViewModel打破WPF中的UI自动化
在我的一些用户控件中,我将Content属性绑定到另一个用户控件的ViewModel。定义了一个数据模板,将ViewModel映射到正确的视图以显示在屏幕上。
这适用于应用程序由用户运行的情况,但如果我尝试在UISpy或其他工具中查看自动化树,它总是停在ViewModel。
我在很多地方都发现了这个问题。有没有办法告诉ViewModel公开它下面树中的任何自动化属性?还是我定义数据模板错了?
由于提前, 敦尔
编辑:这里是从应用程序取得的样品XAML。它阻止访问自动化树。它被放置在一个RibbonWindow中。
<TabControl Grid.Column="2"
cal:RegionManager.RegionName="{x:Static Regions:RegionNames.MainRegion}"
Name="tabControlMain"
SelectedValuePath="Name">
<TabControl.Template>
<ControlTemplate TargetType="TabControl">
<Grid>
<TabPanel IsItemsHost="True" Visibility="Hidden" />
<Border BorderBrush="{DynamicResource BorderBrush}" Background="White" BorderThickness="1">
<framework:CachingContentPresenter
ItemsSource="{Binding Items, ElementName=tabControlMain}"
ContentTemplateSelector="{framework:MvvmTemplateSelector}"
ContentSource="SelectedContent" />
</Border>
</Grid>
</ControlTemplate>
</TabControl.Template>
下面是上述XAML的早期版本。它被允许访问自动化树:
<TabControl
Grid.Column="2"
Padding="0"
cal:RegionManager.RegionName="{x:Static Regions:RegionNames.MainRegion}"
Name="tabControlMain"
TabStripPlacement="Bottom"
SelectedValuePath="Name"
ItemContainerStyle="{StaticResource TabItemStyle}">
</TabControl>
凡TabItemStyle是:
<Style TargetType="{x:Type TabItem}" x:Key="TabItemStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当询问特定的内容是否有错误(在本例中为DataTemplate)时,粘贴XAML或代码总是有帮助的。 – 2010-10-05 13:01:50