[英]How to vertically center a wingding character in textblock xaml / C# / UWP
[英]How to access a named TextBlock in a button XAML C# UWP
我将我的 UWP 应用程序更改为具有响应式 UI。 从那以后,我在将文本放入按钮时遇到了问题。 这是 XAML:
<Page
x:Class="Stock_Manager_Windows.IncomeOrder3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stock_Manager_Windows"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="#FFBBBBBB">
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
...
<Button x:Name="Envoyer" Content="Envoyer" HorizontalAlignment="Stretch" Margin="2,2,0,2" VerticalAlignment="Stretch" Grid.Column="4" Grid.ColumnSpan="2" Grid.Row="4" Background="#249C23" Foreground="White" FontWeight="Bold" Click="buttonSendSelectedItems_Click">
<Button.ContentTemplate>
<DataTemplate x:Name="EnvoyerDataTemplate" >
<Viewbox>
<TextBlock x:Name="EnvoyerTxt" Text="Envoyer"/>
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
...
</Grid>
</Page>
在修改之前,我曾经使用此 C# 代码更新按钮中的文本:
Envoyer.Content = "Envoyer (" + count + ")";
现在这是不可能的......然后一开始我尝试了:
envoyerTxt.Text = "Envoyer (" + count + ")";
我尝试使用通过以下链接解释的解决方案,但代码似乎已过时,不相关,或者我太愚蠢而无法理解。
有人可以向我解释解决方案吗? 也许问题不在 c# 代码中,而是在 XAML 中,我没有任何线索。
在您的ContentTemplate
将TextBlock
更改为此。
<TextBlock x:Name="EnvoyerTxt" Text="{Binding}" />
这意味着现在,无论设置什么内容(文本),它都会绑定到您的模板TextBlock
文本属性,并且会相应地更改。
还要清楚的是,您不能通过模板内的名称访问元素。 因为模板是在运行时而不是在编译时加载的。 这就是为什么在模板内部只有绑定才能分配数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.