简体   繁体   English

ViewModel打破了WPF中的UI自动化

[英]ViewModel breaks UI automation in WPF

I have a WPF application that mostly follows MVVM, which I am trying to automate. 我有一个主要遵循MVVM的WPF应用程序,我正在尝试使其自动化。

In some of my user controls I bind the Content property to the ViewModel of another user control. 在某些用户控件中,我将Content属性绑定到另一个用户控件的ViewModel。 There is a data template defined which maps the ViewModel to the correct View to show on the screen. 定义了一个数据模板,该模板将ViewModel映射到要在屏幕上显示的正确View。

This works fine for when the application is run by a user, but if I try to view the automation tree in UISpy or another tool, it always stops at the ViewModel. 当用户运行该应用程序时,此方法效果很好,但是如果我尝试在UISpy或其他工具中查看自动化树,则它总是在ViewModel处停止。

I have found this problem in numerous places. 我在很多地方都发现了这个问题。 Is there a way to tell the ViewModel to expose any automation properties beneath it in the tree? 有没有办法告诉ViewModel在树中公开它下面的任何自动化属性? or am I defining the data template wrong? 还是我定义数据模板错误?

Thanks in advance, Donal 在此先感谢Donal

EDIT: Here is a sample XAML taken from the app. 编辑:这是从应用程序中提取的示例XAML。 It is blocking accessing the automation tree. 它阻止访问自动化树。 It is placed inside a RibbonWindow. 它放置在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>

Below is a previous version of the above XAML. 下面是上述XAML的早期版本。 It was allowing access to the automation tree: 它允许访问自动化树:

<TabControl 
     Grid.Column="2" 
     Padding="0" 
     cal:RegionManager.RegionName="{x:Static Regions:RegionNames.MainRegion}"  
     Name="tabControlMain" 
     TabStripPlacement="Bottom" 
     SelectedValuePath="Name" 
     ItemContainerStyle="{StaticResource TabItemStyle}">
</TabControl>

Where the TabItemStyle is: 其中TabItemStyle为:

<Style TargetType="{x:Type TabItem}" x:Key="TabItemStyle">
   <Setter Property="Template">
      <Setter.Value>
          <ControlTemplate TargetType="TabItem">
          </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

I managed to find a solution to the XAML posted above. 我设法找到了上面发布的XAML的解决方案。 I'm not sure if it will work in all cases in our app, but I have yet to try them properly. 我不确定在我们的应用程序中它是否在所有情况下都能正常工作,但我尚未对其进行尝试。

Anyway, the fix was found in this post: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fa8eb86f-5001-4af6-adb3-ceb0799a0cf3/ 无论如何,此帖子中找到了修复程序: http : //social.msdn.microsoft.com/Forums/en-US/wpf/thread/fa8eb86f-5001-4af6-adb3-ceb0799a0cf3/

Basically, we added 'Name="PART_SelectedContentHost"' to the CachingContentPresenter in the ControlTemplate. 基本上,我们在ControlTemplate中的CachingContentPresenter中添加了“ Name =“ PART_SelectedContentHost””。 From my understanding, this PART_ tells the parser/compiler to include the default behaviour of the control template such as mouse events and keyboard presses as well as Automation properties. 据我了解,该PART_告诉解析器/编译器包括控件模板的默认行为,例如鼠标事件和键盘按下以及Automation属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM