-
May 28th, 2009, 08:10 AM
#1
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
-
May 28th, 2009, 08:32 PM
#2
Re: WPF Button Content Multi-line
I'd start my investigation looking over these solutions: found here.
-
May 29th, 2009, 03:51 PM
#3
Re: WPF Button Content Multi-line
Originally Posted by Arjay
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
-
May 29th, 2009, 05:50 PM
#4
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.
-
May 30th, 2009, 10:53 AM
#5
Re: WPF Button Content Multi-line
Originally Posted by Arjay
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
-
May 30th, 2009, 11:54 AM
#6
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
-
June 1st, 2009, 06:11 PM
#7
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>
-
June 4th, 2009, 04:38 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|