다음을 통해 공유


방법: 테두리 속성 변경

업데이트: 2007년 11월

이 예제에서는 Border 요소의 Background 색을 변경하는 방법을 보여 줍니다.

예제

다음 예제에서는 Button 요소를 Canvas의 중심 가까이에 배치합니다. 요소 주위에 테두리를 표시하기 위해 CanvasBorder 요소 안에 중첩됩니다. Button 요소를 클릭하면 Border 요소의 Brush 색이 LightSteelBlue로 바뀝니다. 또한 클릭 이벤트는 TextBlock의 텍스트 문자열을 Canvas(이벤트 발생을 알림)에 추가하고 Button 요소의 텍스트 내용을 업데이트합니다.

<Window
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Border_change_programmatic.Window1"
  Title="Change Border Programmatically">
  <Border Name="root"
          BorderThickness="2"
            BorderBrush="Black"
            Background="LightGray"
            Width="350"
            Height="350">
    <Canvas>
      <Button Name="btn" Canvas.Top="40" Canvas.Left="40" Background="LightSkyBlue" Height="35" Click="ChangeBG">Click Me to change the Background Color</Button>
      <TextBlock Canvas.Top="130" Canvas.Left="40" Name="Text1">Waiting for Click!</TextBlock>
    </Canvas>
  </Border>
</Window>

다음 코드 숨김 파일이 ChangeBG 메서드를 처리합니다.

Private Sub ChangeBG(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
    root.Background = System.Windows.Media.Brushes.LightSteelBlue
    btn.Content = "Clicked!"
    Text1.Text = "The background is now LightSteelBlue"
End Sub
     void ChangeBG(object sender, System.Windows.RoutedEventArgs e)   
            {
            root.Background = System.Windows.Media.Brushes.LightSteelBlue;
            btn.Content = "Clicked!";
            Text1.Text = "The background is now LightSteelBlue"; 
            }

참고 항목

개념

Panel 개요

참조

Border