문제

나는 .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> 밑줄이 있는 정확한 텍스트를 인쇄합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top