[英]Multiline Text in a WPF Button
如何仅使用 C# 在 WPF 按钮上获取多行文本? 我看过在 XAML 中使用<LineBreak/>
示例,但我的按钮完全是在 C# 中以编程方式创建的。 按钮上的数字和标签对应于域模型中的值,所以我认为我不能使用 XAML 来指定它。
我尝试了下面的幼稚方法,但它不起作用。
Button b = new Button();
b.Content = "Two\nLines";
要么
b.Content = "Two\r\nLines";
在任何一种情况下,我看到的都是文本的第一行(“两个”)。
或直接在 XAML 中:
<Button>
<TextBlock>Two<LineBreak/>Lines</TextBlock>
</Button>
我更喜欢这种方式:
<Button Width="100">
<TextBlock TextWrapping="Wrap">This is a fairly long button label</TextBlock>
</Button>
它对我有用。
答案很简单。 只需使用

引入换行符,即:
<Button Content="Row 1 Text 
 Row 2 Text"/>
有几种方法可以通过 XAML 执行此操作:
<Button> <TextBlock TextAlignment="Center">Line 1<LineBreak/>Line 2</TextBlock> </Button>
这种方法很简单,但是没有办法轻松控制文本的对齐方式:
<Button Content="Line 1 
 Line 2"/>
一旦 Buttons 的大小小于 TextBlocks 的大小,它就会自动将内容分成两行或更多行
<Button> <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center">Line 1 Line 2</TextBlock> </Button>
<Button> <StackPanel> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </StackPanel> </Button>
<Button> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </Grid> </Button>
这就是我们在这里做的方式,它也允许轻松居中
<Button Height="40" Width="75">
<StackPanel>
<TextBlock Text="Line1" HorizontalAlignment="Center"/>
<TextBlock Text="Line2" HorizontalAlignment="Center"/>
</StackPanel>
</Button>
原来“\\n”工作正常。 我的网格有一个固定的大小,按钮中根本没有视觉指示有更多可用的文本(例如,没有“...”表示截止)。 一旦我慷慨地扩大了我的网格的大小,按钮文本就会显示为两行。
你试过这个吗?
b.Content = new TextBlock {
Text = "Two\nLines",
TextWrapping = TextWrapping.Wrap };
如果这不起作用,那么您可以尝试将 StackPanel 添加为子项并向其添加两个 TextBlock 元素。
怎么样:
TextBlock textBlock = new TextBlock();
textBlock.Inlines.Add("Two");
textBlock.Inlines.Add(new LineBreak());
textBlock.Inlines.Add("Lines");
Button button = new Button();
button.Content = textBlock;
如果你使用的是 C# 3,你可以让它稍微整洁一点:
Button button = new Button
{
Content = new TextBlock { Inlines = { "Two", new LineBreak(), "Lines" } }
};
我遇到过同样的问题。
我试过了:
- button.content = "Line1\\nLine2"(没用,可能是我做错了什么);
- 用新标签替换按钮文本(不允许您将文本居中对齐);
- 用文本块替换按钮文本(我认为这可以让您居中对齐文本但不会包装它);
我见过提到使用堆栈面板或网格的答案。
我看到答案说不要使用Textbox 。
即使 OP 说\\n
有效,我也不认为这是要走的路,在我看来,您只是强制控制执行您想要的操作,并且如果在任何时候您需要更改text 你必须去检查文本是否正确换行或者\\n
需要在另一个位置。
我发现最好的方法(对我来说)是用文本框替换按钮内容(您只需拖放,无需弄乱 XAML)并按照说明设置以下属性:IsReadOnly = true ;
Focusable = false(可选);
我认为Focusable = false
阻止用户选择文本,即使他无法编辑它,我也不希望他选择它(个人品味)。
这将使文本框的行为类似于标签,但具有让您居中对齐文本的优势。
试试下面的代码:
<Button Command="{Binding DeliveryDateCommand}" x:Name="cntlbtnf5" Background="White" BorderBrush="#FFABADB3" Grid.Column="4">
<Border BorderBrush="{x:Null}" Height="Auto">
<TextBlock Text="Ctrl + F5 Delivery BillDate" TextWrapping="Wrap" Width="110" TextAlignment="Center" Height="44" />
</Border>
</Button>
作为替代/解决方法,如果您必须使用无法轻松编辑其内容的外部自定义按钮,您始终可以使用网格并将按钮的功能部分覆盖在您的视觉对象上(如果它可以是静态的)。
<Grid>
<TextBlock Foreground="LightGray">Click<LineBreak />Here</TextBlock>
<MyCustomButton BorderBrush="Transparent" IsPressed="{Binding MyBtn.IsPressed, Mode=TwoWay}" />
</Grid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.