첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ FrameworkElement 클래스 : FindName 메소드를 사용해 명칭으로 엘리먼트 찾기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800"

    Height="600"

    Title="FrameworkElement 클래스 : FindName 메소드를 사용해 명칭으로 엘리먼트 찾기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Window.Resources>

        <Style TargetType="{x:Type Button}">

            <Setter Property="Margin"          Value="5" />

            <Setter Property="BorderThickness" Value="3" />

        </Style>

    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*" />

            <ColumnDefinition Width="*" />

            <ColumnDefinition Width="*" />

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="*" />

            <RowDefinition Height="*" />

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <Button Name="A" Grid.Row="0" Grid.Column="0" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="B" Grid.Row="0" Grid.Column="1" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="C" Grid.Row="0" Grid.Column="2" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="D" Grid.Row="1" Grid.Column="0" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="E" Grid.Row="1" Grid.Column="1" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="F" Grid.Row="1" Grid.Column="2" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="G" Grid.Row="2" Grid.Column="0" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="H" Grid.Row="2" Grid.Column="1" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

        <Button Name="I" Grid.Row="2" Grid.Column="2" Content="{Binding RelativeSource={RelativeSource self}, Path=Name}" />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Loaded += Window_Loaded;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

 

        /// <summary>

        /// 윈도우 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            Button button = FindName("E") as Button;

 

            button.Background = Brushes.Blue;

            button.Foreground = Brushes.White;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요