如何:以程式設計方式變更 TextWrapping 屬性
範例
下列程式碼範例示範如何以程式設計方式變更 屬性的值 TextWrapping 。
三 Button 個 StackPanel 元素會放在 XAML 中的 元素內。 的每個 Click 事件 Button 都會與程式碼中的事件處理常式相對應。 事件處理常式會使用與按一下按鈕時所套用 txt2
的值相同的名稱 TextWrapping 。 此外,中的 txt1
文字也會更新 , TextBlock 以反映 屬性中的變更。
<StackPanel Orientation="Horizontal" Margin="0,0,0,20">
<Button Name="btn1" Background="Silver" Width="100" Click="Wrap">Wrap</Button>
<Button Name="btn2" Background="Silver" Width="100" Click="NoWrap">NoWrap</Button>
<Button Name="btn4" Background="Silver" Width="100" Click="WrapWithOverflow">WrapWithOverflow</Button>
</StackPanel>
<TextBlock Name="txt2" TextWrapping="Wrap" Margin="0,0,0,20" Foreground="Black">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit.Lorem ipsum dolor sit aet, consectetuer adipiscing elit.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</TextBlock>
private void Wrap(object sender, RoutedEventArgs e)
{
txt2.TextWrapping = System.Windows.TextWrapping.Wrap;
txt1.Text = "The TextWrap property is currently set to Wrap.";
}
private void NoWrap(object sender, RoutedEventArgs e)
{
txt2.TextWrapping = System.Windows.TextWrapping.NoWrap;
txt1.Text = "The TextWrap property is currently set to NoWrap.";
}
private void WrapWithOverflow(object sender, RoutedEventArgs e)
{
txt2.TextWrapping = System.Windows.TextWrapping.WrapWithOverflow;
txt1.Text = "The TextWrap property is currently set to WrapWithOverflow.";
}
Private Sub Wrap(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
txt2.TextWrapping = System.Windows.TextWrapping.Wrap
txt1.Text = "The TextWrap property is currently set to Wrap."
End Sub
Private Sub NoWrap(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
txt2.TextWrapping = System.Windows.TextWrapping.NoWrap
txt1.Text = "The TextWrap property is currently set to NoWrap."
End Sub
Private Sub WrapWithOverflow(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
txt2.TextWrapping = System.Windows.TextWrapping.WrapWithOverflow
txt1.Text = "The TextWrap property is currently set to WrapWithOverflow."
End Sub
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應