如何使用一个WPF按钮的ACCESSKEY使用自定义的ContentTemplate? [英] How to use an accesskey on a WPF Button with a custom ContentTemplate?
问题描述
方案:
目前我有这样的XAML code:
Currently I have this XAML code:
<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)与文本块组合无法正常工作。其实,我觉得文本块应该是一个内容presenter(下同),但这种崩溃我的Visual Studio每次2010实例。
Obviously the accesskey (the 'c' key: _Cancel) doesn't work in combination with the TextBlock. I actually think the TextBlock should be a ContentPresenter (below), but this crashes my Visual Studio 2010 instance every time.
<ContentPresenter Margin="10,0,10,0" RecognizesAccessKey="True" />
问:
- 什么是使用accesskeys上一个WPF按钮带有的ContentTemplate的最佳解决方案?
在此先感谢!
推荐答案
而不是的TextBlock
使用 AccessText
是这样的:
Instead of TextBlock
use AccessText
thus:
<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>
PS。 内容presenter
应该在控件模板
来根据显示内容的DataTemplate中使用
。如果您在使用它的DataTemplate
它会导致无限递归的的DataTemplate
被调用一遍又一遍。
PS. ContentPresenter
should be used inside a ControlTemplate
to display content according to a DataTemplate
. If you use it within a DataTemplate
it causes infinite recursion as the DataTemplate
is invoked over and over again.
这篇关于如何使用一个WPF按钮的ACCESSKEY使用自定义的ContentTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!