WPF 레이블 가속기 키 비활성화(텍스트 밑줄이 누락됨)
-
09-06-2019 - |
문제
나는 .Content
밑줄이 포함된 문자열에 대한 Label 값입니다.첫 번째 밑줄은 액셀러레이터 키로 해석됩니다.
기본 문자열을 변경하지 않고(모두 교체하여) _
~와 함께 __
), 라벨용 가속기를 비활성화하는 방법이 있나요?
해결책
TextBlock을 레이블의 콘텐츠로 사용하는 경우 해당 텍스트는 밑줄을 흡수하지 않습니다.
다른 팁
레이블의 기본 템플릿에 있는 ContentPresenter의 RecognizesAccessKey 속성을 재정의할 수 있습니다.예를 들어:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.Resources>
<Style x:Key="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" TargetType="Label">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Label>_This is a test</Label>
</Grid>
</Page>
왜 이렇게 하면 안 되나요?
public partial class LabelEx : Label
{
public bool PreventAccessKey { get; set; } = true;
public LabelEx()
{
InitializeComponent();
}
public new object Content
{
get
{
var content = base.Content;
if (content == null || !(content is string))
return content;
return PreventAccessKey ?
(content as string).Replace("__", "_") : content;
}
set
{
if (value == null || !(value is string))
{
base.Content = value;
return;
}
base.Content = PreventAccessKey ?
(value as string).Replace("_", "__") : value;
}
}
}
사용 <TextBlock> ... </TextBlock>
대신에 <Label> ... </Label>
밑줄이 있는 정확한 텍스트를 인쇄합니다.
제휴하지 않습니다 StackOverflow