■ XAML에서 내장 코드 사용하기

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


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="Petzold.EmbedCodeInXaml.EmbeddedCodeWindow"

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

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

    Width="800"

    Height="600"

    Title="XAML에서 내장 코드 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16"

    Loaded="Window_Loaded">

    <StackPanel>

        <Button

            HorizontalAlignment="Center"

            Margin="24"

            Click="button_Click">

            Click the Button

        </Button>

        <Ellipse Name="ellipse"

            Width="200"

            Height="100"

            Margin="24"

            Stroke="Red"

            StrokeThickness="10" />

        <ListBox Name="listBox"

            Width="150"

            Height="150"

            Margin="24"

            SelectionChanged="listBox_SelectionChanged" />

        <x:Code>

<![CDATA[

 

private void Window_Loaded(object sender, RoutedEventArgs e)

{

    foreach(System.Reflection.PropertyInfo propertyInfo in typeof(Brushes).GetProperties())

    {

        this.listBox.Items.Add(propertyInfo.Name);

    }

}

 

private void button_Click(object sender, RoutedEventArgs e)

{

    Button button = sender as Button;

 

    MessageBox.Show("The button labeled '" + button.Content + "' has been clicked.");

}

 

private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

    string item = this.listBox.SelectedItem as string;

 

    System.Reflection.PropertyInfo propertyInfo = typeof(Brushes).GetProperty(item);

 

    ellipse.Fill = (Brush)propertyInfo.GetValue(null, null);

}

 

]]>

        </x:Code>

    </StackPanel>

</Window>

 

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

Posted by 사용자 icodebroker
TAG