CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    WPF Button Content Multi-line

    I have a button template. I want the button text to be multiline, but I have no idea how to set this.

    I know I can set this in the xaml where I create the button by adding a text block and using <LineBreak/> but this overrides the TextBlock used by the button template.

    E.G: The following would work, but the text block defined replaces the text block used by the template. This is no good.
    Code:
    <Button Template="{StaticResource MenuButtonTemplate}">
         <TextBlock>
              Project<LineBreak/>Management
         </TextBlock>
    </Button>
    E.G: The following is my button template:
    Code:
      <ControlTemplate x:Key="MenuButtonTemplate" TargetType="{x:Type Button}">
            <Border Name="MenuButtonBorder" 
                    BorderBrush="Black" 
                    BorderThickness="1"
                    CornerRadius="3"
                    Background="Transparent"
                    TextBlock.Foreground="White"
                    TextBlock.FontSize="12"
                    TextBlock.TextAlignment="Center"
                    Width="80"
                    Margin="3,3,3,3">
                <Grid>
                    <Rectangle Name="MenuButtonFocusCue" 
                               Visibility="Hidden"
                               Stroke="LightSteelBlue"
                               StrokeThickness="1"
                               SnapsToDevicePixels="True">
                    </Rectangle>
                    <ContentPresenter RecognizesAccessKey="True"
                                      Margin="2,2,2,2"
                                      VerticalAlignment="Center">
                    </ContentPresenter>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="MenuButtonBorder"
                            Property="Background"
                            Value="Transparent"/>
                    <Setter TargetName="MenuButtonBorder"
                            Property="TextBlock.Foreground"
                            Value="Yellow"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="MenuButtonBorder"
                            Property="Background"
                            Value="Transparent"/>
                    <Setter TargetName="MenuButtonBorder"
                            Property="TextBlock.Foreground"
                            Value="Yellow"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter TargetName="MenuButtonFocusCue" 
                            Property="Visibility"
                            Value="Visible"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    Any suggestions would be appreciated.

    Mike B

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF Button Content Multi-line

    I'd start my investigation looking over these solutions: found here.

  3. #3
    Join Date
    Feb 2001
    Posts
    2,455

    Re: WPF Button Content Multi-line

    Quote Originally Posted by Arjay View Post
    I'd start my investigation looking over these solutions: found here.
    Arjay

    That was unexpected from you and uncool...... Anyway, I did search google first, I also looked through my Pro WPF in C# 2008 book and I could not find the answer.

    I want the text block in a button template to be set to multi-line. I did try text wrap, but when I specify TextBlock.TextWrapping, I get an error saying property cannot be found.

    If you have any valid suggestions, I wouldn't mind reading them.

    Thanks

    Mike B

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF Button Content Multi-line

    You know we answer a lot of questions around here so it's pretty hard to keep track of those too busy to look things up for themselves.

    So help us out here. If you've looked in google, how about stating why the solutions found in a google search "WPF text multiline" aren't adequate.

  5. #5
    Join Date
    Feb 2001
    Posts
    2,455

    Re: WPF Button Content Multi-line

    Quote Originally Posted by Arjay View Post
    You know we answer a lot of questions around here so it's pretty hard to keep track of those too busy to look things up for themselves.

    So help us out here. If you've looked in google, how about stating why the solutions found in a google search "WPF text multiline" aren't adequate.
    Arjay,

    Creating multi-line text is not the problem. Creating multi-line button text is not the problem. The problem is creating a button template in a resource file where the content presenter supports text wrapping. This I cannot find on google. In fact, if I type WPF Multiline button, this post is near the top....

    Why should I have to add <LineBreak/> or create a new TextBlock for my buttons every time I use them simply to support text wrapping? I should just be able to add this to my Button Template and be done with it. This is my problem.

    Mike B

  6. #6
    Join Date
    Feb 2001
    Posts
    2,455

    Re: WPF Button Content Multi-line

    Alright, well, I thought I found the solution, but it doesn't work...... I should have searched for ContentPresenter then I would have known that this is actually a place holder for the content and for lack of a better description means "YOUR CONTENT GOES HERE!".

    So, I added the TextBlock to my template and placed the ContentPresenter there, but......not wrapping.

    Code:
    <TextBlock TextAlignment="Center"
                               VerticalAlignment="Center"
                               Foreground="White"
                               FontSize="12"
                               TextWrapping="Wrap">
                        <ContentPresenter RecognizesAccessKey="True"/>
                    </TextBlock>
    Mike B

  7. #7
    Join Date
    Jan 2009
    Posts
    36

    Re: WPF Button Content Multi-line

    I think that you can set the ContentPresenter's ContentTemplate like so:

    Code:
    <ContentPresenter RecognizesAccessKey="True"
              Margin="2,2,2,2"
              VerticalAlignment="Center">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding}" TextWrapping="Wrap" Height="Auto" Width="Auto" />
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

  8. #8
    Join Date
    Jun 2009
    Posts
    1

    Re: WPF Button Content Multi-line

    I have another question about accessing TextBlock, which is inside a button template.
    there is a template in xaml:
    Code:
    <controltemplate x:key="buttempl1" targettype="{x:type button}">
      <border>
         <grid>
           <textblock name="tx1" text="asd"/>
         </grid>
      </border>
    </controltemplate>
    and the button
    Code:
    <button name="btn1" template="{dynamicresource buttempl1} />
    How can I dynamically change tx1.Text in vb code for this button?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured