次の方法で共有


方法 : TextWrapping プロパティをプログラムにより変更する

更新 : 2007 年 11 月

使用例

TextWrapping プロパティの値を、Microsoft Visual Basic .NET を使用してプログラムによって変更する方法を、次のコード例に示します。

4 つの Buttons を、StackPanel 要素内に Extensible Application Markup Language (XAML) で配置しています。各 ButtonClick イベントは、以下に示す Microsoft Visual Basic .NET コードで定義されているサブプロシージャに対応しています。各イベントに関連付けられた TextWrapping 値が呼び出されると、txt2 という Name で識別される 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>

上で定義されている Button クリックに関連付けられたイベントは、Microsoft Visual Basic .NET のサブプロシージャで処理されます。Click が発生するたびに、TextWrapping プロパティの値がプロシージャで指定された値に変更され、プロパティの変更を反映するために要素 txt1 が更新されます。

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
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.";
}

参照

参照

TextWrapping

TextWrapping