[WPF][Border] 背景色を設定する方法

スポンサーリンク

概要

この記事では、WPFのBorderコントロールを使用して背景色を設定する方法について説明します。

 

構文

XAMLの構文

<Border Background="色" />

BorderコントロールのBackgroundプロパティに色を指定することで、背景色を設定することができます。

コードビハインドの構文

border.Background = new SolidColorBrush(Colors.色);

コードビハインドで背景色を設定する場合は、BorderコントロールのBackgroundプロパティにSolidColorBrushクラスのインスタンスを代入します。

 

使用例

XAMLの例

<Border Background="LightBlue" BorderBrush="Black" BorderThickness="2" 
        CornerRadius="10" Margin="5">
  <TextBlock Text="Hello, World!"
         HorizontalAlignment="Center"
             VerticalAlignment="Center" />
</Border>

この例では、Borderコントロールの背景色をLightBlueに設定しています。

コードビハインドの例

Border border = new Border(); 
border.BorderBrush = Brushes.Black;
border.BorderThickness = new Thickness(2); 
border.CornerRadius = new CornerRadius(10); 
border.Background = new SolidColorBrush(Colors.LightBlue);
TextBlock textBlock = new TextBlock(); 
textBlock.Text = "Hello, World!"; 
textBlock.HorizontalAlignment = HorizontalAlignment.Center; 
textBlock.VerticalAlignment = VerticalAlignment.Center; 
border.Child = textBlock;

この例では、コードビハインドでBorderコントロールの背景色をLightBlueに設定しています。

実行例

実行例

まとめ

Borderコントロールを使用して背景色を設定する方法について説明しました。XAMLではBackgroundプロパティに色を指定し、コードビハインドではSolidColorBrushクラスのインスタンスを使用して背景色を設定します。

Please follow and like us:

コメント

タイトルとURLをコピーしました