【问题标题】:How to use an accesskey on a WPF Button with a custom ContentTemplate?如何在带有自定义 ContentTemplate 的 WPF 按钮上使用 accesskey?
【发布时间】:2011-09-14 12:27:19
【问题描述】:

场景:

目前我有这个 XAML 代码:

<Button Content="_Cancel" IsCancel="True" Command="{Binding Path=CancelCommand}" Margin="5">
   <Button.ContentTemplate>
      <DataTemplate>
         <TextBlock Margin="10,0,10,0" />
      </DataTemplate>
   </Button.ContentTemplate>
</Button>

显然 accesskey('c' 键:_Cancel)不能与 TextBlock 结合使用。我实际上认为 TextBlock 应该是 ContentPresenter(如下),但这每次都会使我的 Visual Studio 2010 实例崩溃。

<ContentPresenter Margin="10,0,10,0" RecognizesAccessKey="True" />

问题:

  • 在带有 ContentTemplate 的 WPF 按钮上使用 accesskey 的最佳解决方案是什么?

提前致谢!

【问题讨论】:

    标签: c# .net wpf xaml access-keys


    【解决方案1】:

    而不是TextBlock 使用AccessText 因此:

    <Button Content="_Cancel" IsCancel="True" Command="{Binding Path=CancelCommand}" Margin="5">
       <Button.ContentTemplate>
          <DataTemplate>
             <AccessText Margin="10,0,10,0" Text="{Binding}"/>
          </DataTemplate>
       </Button.ContentTemplate>
    </Button>
    

    附言。 ContentPresenter 应在 ControlTemplate 内使用,以根据 DataTemplate 显示内容。如果你在 DataTemplate 中使用它,它会导致无限递归,因为 DataTemplate 被一遍又一遍地调用。

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多