我想知道如何在WPF按钮中垂直显示文本。我有一个高大的矩形按钮,宽度= 38,高度= 90.我希望按钮将文本显示为
B
U
ÿ
或
S
E
L
L
任何人都可以告诉我如何在WPF中实现这一目标。
由于
答案 0 :(得分:5)
您可以简单地使用ItemsControl,它将在其中创建元素列表,使它们显示为垂直。
<Button>
<ItemsControl ItemsSource="BUY" />
</Button>
您可以在this问题..
上找到有关执行此类操作的更多信息答案 1 :(得分:0)
也许这个答案会帮助你。 你基本上需要一种风格来包装添加到按钮的文本。
看看这个答案: WPF button textwrap style
答案 2 :(得分:0)
使用ItemsControl的答案听起来很完美,但如果你不想这样做,你可以创建一个转换器,添加一个&#39; \ n&#39;在每个文字之间。
您网页的XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DilbertDownloader" x:Class="DilbertDownloader.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:TextVerticalConverter x:Key="TextVerticalConverter"/>
</Window.Resources>
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<Button Content="{Binding ButtonText, Converter={StaticResource TextVerticalConverter}}" HorizontalAlignment="Left" Margin="270,156,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
转换器的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DilbertDownloader
{
public class TextVerticalConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is string)) return value;
var converted = "";
foreach (var character in (string)value)
{
//If the character is a space, add another new line
//so we get a vertical 'space'
if (character == ' ') {
converted += '\n';
continue;
}
//If there's a character before this one, add a newline before our
//current character
if (!string.IsNullOrEmpty(converted))
converted += "\n";
converted += character;
}
return converted;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//Not really worried about this at the moment
throw new NotImplementedException();
}
}
}
ViewModel的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DilbertDownloader
{
public class ViewModel
{
public string ButtonText { get; set; }
public ViewModel()
{
ButtonText = "BUY";
}
}
}
希望有用
答案 3 :(得分:0)
使用LineBreak和Literal LineBreak
<Button Width="38" Height="90" Content="B
u
y"/>
<Button Width="38" Height="90">
<TextBlock>
<Run Text="S"/>
<LineBreak/>
<Run Text="E"/>
<LineBreak/>
<Run Text="L"/>
<LineBreak/>
<Run Text="L"/>
<LineBreak/>
</TextBlock>
</Button>
将宽度和文本换行设置为文本块
<Button Width="38" Height="90">
<TextBlock Text="Buy" Width="8" TextWrapping="Wrap"></TextBlock>
</Button>