0

I can able to create access key for button content during Design time but unable to create during run time

enter image description here

btnContent -- "C&ancel" -- it works perfect

btnContent -- "E&xit" --- not working, when i try to give acces key for letter 'x'

i have also tried with (_)underscore and "Exit" ,but no luck. i jus need to underline 'x' in string "Exit".

Below is the style for my button

     <Style x:Key="LTC_ButtomForm_A" TargetType="{x:Type Button}">
                <Style.Resources>
                    <converter:ConverterFirstPartTextButton x:Key="ConverterFirstPartTextButton" />
                    <converter:ConverterUnderlineTextButton x:Key="ConverterUnderlineTextButton" />
                    <converter:ConverterLastPartTextButton x:Key="ConverterLastPartTextButton" />
                </Style.Resources>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="Fondo" Margin="0" Padding="10,0" Background="#FF253A94" BorderBrush="#00000000" BorderThickness="1" CornerRadius="5">

    Text="{TemplateBinding Content,Converter={StaticResource ConverterFirstPartTextButton}}" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center">
<Run Text="{TemplateBinding Content, Converter={StaticResource ConverterUnderlineTextButton}}" TextDecorations="Underline" />
<Run Text="{TemplateBinding Content, Converter={StaticResource ConverterLastPartTextButton}}" />
 </TextBlock>
         </Border>
     </ControlTemplate>
     </Setter.Value>
     </Setter>
     </Style>

where ConverterFirstPartTextButton

Public Class ConverterFirstPartTextButton
    Implements IValueConverter

#Region "IValueConverter Members"

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object _
        Implements IValueConverter.Convert
        Dim part As String = String.Empty
        If value IsNot Nothing Then
            part = value.ToString().Split("&").FirstOrDefault()
        End If
        Return part
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) _
        As Object Implements IValueConverter.ConvertBack
        Return value
    End Function

#End Region
End Class

ConverterUnderlineTextButton

Public Class ConverterUnderlineTextButton
    Implements IValueConverter

#Region "IValueConverter Members"

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object _
        Implements IValueConverter.Convert
        Dim underlineLetter As String = String.Empty
        If value IsNot Nothing Then
            underlineLetter = value.ToString().Split("&").LastOrDefault
        End If
        If underlineLetter = value Then
            Return String.Empty
        Else
            underlineLetter = underlineLetter.Substring(0, 1)
            Return underlineLetter
        End If
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) _
        As Object Implements IValueConverter.ConvertBack
        Return value
    End Function

#End Region
End Class

ConverterLastPartTextButton

Public Class ConverterLastPartTextButton
    Implements IValueConverter

#Region "IValueConverter Members"

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object _
        Implements IValueConverter.Convert
        Dim part As String = String.Empty
        If value IsNot Nothing Then
            part = value.ToString().Split("&").LastOrDefault
        End If
        If part <> value And part.Length > 1 Then
            part = part.Substring(1)
            Return part
        Else
            Return String.Empty
        End If
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) _
        As Object Implements IValueConverter.ConvertBack
        Return value
    End Function

#End Region
End Class

these converts main use is to underline the given text and its working Perfect during initialization. But when i change the content during run time it shows as above image(fig 2).

Selva
  • 1,310
  • 2
  • 14
  • 31
  • it will help http://stackoverflow.com/questions/3056008/wpf-underline-a-letter-in-a-label-element – Anton Nov 27 '13 at 11:57
  • @Anton, thanx for the reply. have checked with the given above link its not working.. Is there any other way doin via code? – Selva Nov 27 '13 at 13:15

1 Answers1

0

Look I don't really understand what is your problem. Anyway, I created a simple wpf application and put a button on a grid.

 <Grid>
        <Button Name="btnButton1" Width="150" Height="70" Click="btnButton1_Click" />
 </Grid>

After in code

 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
    btnButton1.Content = "E_xit";
 }

So now when I keep Alt pressed the letter x is underscored. If when Alt is pressed I press x then the btnButton1_Click event is fired and I see a message box.

private void btnButton1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("1111");
}

As you see I have created the content in code.

P.S. If you are using a custom content template then read this custom content template

If you just need to make one letter underlined then

            StackPanel stackPanel = new StackPanel();
            stackPanel.Orientation = Orientation.Horizontal;
            stackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

            TextBlock textBlock = new TextBlock();
            textBlock.Text = "E";
            stackPanel.Children.Add(textBlock);

            textBlock = new TextBlock();
            textBlock.TextDecorations = System.Windows.TextDecorations.Underline;
            textBlock.Text = "x";
            stackPanel.Children.Add(textBlock);

            textBlock = new TextBlock();
            textBlock.Text = "it";
            stackPanel.Children.Add(textBlock);

            btnButton1.Content = stackPanel;

Just use it in any event handler!

Community
  • 1
  • 1
Anton
  • 731
  • 4
  • 5