|
|
3 데이터 바인딩 기초
가) 소스파일
Silverlight Xaml 소스 : 간단한 디자인을 한 소스이다. <Grid x:Name="LayoutRoot" Width="400" Height="300"> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="300"/> </Grid.ColumnDefinitions>
<Rectangle Stroke="Black" Grid.ColumnSpan="2" Grid.RowSpan="4"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="Beige" Offset="0"/> <GradientStop Color="Black" Offset="1"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>
<TextBlock Text="ID" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" /> <TextBlock Text="Email" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Grid.Row="1" /> <TextBlock Text="Name" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Grid.Row="2" />
<TextBlock Grid.Column="1" Text="ID" x:Name="tdId" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Margin="20,0,0,0" /> <TextBlock Grid.Row="1" Grid.Column="2" Text="Email" x:Name="tdEmail" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Margin="20,0,0,0" /> <TextBlock Grid.Row="2" Grid.Column="3" Text="Name" x:Name="tdName" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Margin="20,0,0,0" />
<Button Grid.Column="0" Grid.Row="3" Content="가져오기" x:Name="btGetUserData" VerticalAlignment="Center" Margin="20,0,0,0" /> </Grid> |
Silverlight C#코드 btGetUserData버튼이 누를때마다 유저데이터를 가져오는 소스. public partial class MainPage : UserControl { UserDataGenerator udg = new UserDataGenerator(); public MainPage() { InitializeComponent(); btGetUserData.Click += new RoutedEventHandler(btGetUserData_Click); // 버튼클릭시 발생하는 이벤트 핸들러생성. }
void btGetUserData_Click(object sender, RoutedEventArgs e) { UserData ud = udg.GetUserData();
tdId.Text = ud.ID; tdEmail.Text = ud.Email; tdName.Text = ud.Name; } } |
UserData Class 소스 : 간단한 유저데이터 클래스 public class UserData { public string ID { get; set; } public string Email { get; set; } public string Name { get; set; }
} |
UserDataGenerator 소스 : 유저데이터를 초기화 하고 유저데이터를 저장 / 가져오는 클래스 public class UserDataGenerator { private IList<UserData> userDatas; private int count = 0; public UserDataGenerator() { userDatas = new List<UserData>();
UserData userData = new UserData(); userData.ID = "test1"; userData.Email = "test1@mail.com"; userData.Name = "은호다"; userDatas.Add(userData); //유저데이터를 생성하여 리스트에 저장. UserData userData2 = new UserData(); userData2.ID = "test2"; userData2.Email = "test2@mail.com"; userData2.Name = "은호일지도"; userDatas.Add(userData2); //유저데이터를 생성하여 리스트에 저장. UserData userData3 = new UserData(); userData3.ID = "test3"; userData3.Email = "test3@mail.com"; userData3.Name = "은호일꺼야"; userDatas.Add(userData3); //유저데이터를 생성하여 리스트에 저장. }
public UserData GetUserData() { count++; if (count == userDatas.Count) { count = 0; } return userDatas[count]; } } |
|
나) 실행장면
< 초기화면 > |
< 버튼을 누를때마다 변경되는 화면 > |