ViewModel 打破了 WPF 中的 UI 自动化

发布于 2024-09-25 19:20:46 字数 1996 浏览 4 评论 0原文

我有一个主要遵循 MVVM 的 WPF 应用程序,我正在尝试将其自动化。

在我的一些用户控件中,我将 Content 属性绑定到另一个用户控件的 ViewModel。定义了一个数据模板,它将 ViewModel 映射到正确的视图以显示在屏幕上。

当用户运行应用程序时,这工作得很好,但如果我尝试在 UISpy 或其他工具中查看自动化树,它总是停在 ViewModel 处。

我在很多地方都发现了这个问题。有没有办法告诉 ViewModel 在树中公开其下方的任何自动化属性?或者我定义的数据模板错误?

提前致谢, Donal

编辑:这是从应用程序中获取的示例 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>

I have a WPF application that mostly follows MVVM, which I am trying to automate.

In some of my user controls I bind the Content property to the ViewModel of another user control. There is a data template defined which maps the ViewModel to the correct View to show on the screen.

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.

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? or am I defining the data template wrong?

Thanks in advance,
Donal

EDIT: Here is a sample XAML taken from the app. It is blocking accessing the automation tree. It is placed inside a 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. 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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-10-02 19:20:46

我设法找到了上面发布的 XAML 的解决方案。我不确定它是否适用于我们应用程序中的所有情况,但我还没有正确尝试它们。

无论如何,在这篇文章中找到了修复:
http://social. msdn.microsoft.com/Forums/en-US/wpf/thread/fa8eb86f-5001-4af6-adb3-ceb0799a0cf3/

基本上,我们将“Name="PART_SelectedContentHost"”添加到 ControlTemplate 中的 CachingContentPresenter 中。根据我的理解,这个 PART_ 告诉解析器/编译器包含控件模板的默认行为,例如鼠标事件和键盘按下以及自动化属性。

I managed to find a solution to the XAML posted above. 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/

Basically, we added 'Name="PART_SelectedContentHost"' to the CachingContentPresenter in the ControlTemplate. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文