17. UserControl
01.
더보기
1.
2.
d:DesignHeight="80"
d:DesignWidth="380">
<Grid Background="#AAe5e5e5">
<StackPanel Margin="4">
<TextBlock Text="Name LastName"
FontSize="20"
FontWeight="Bold" />
<TextBlock Text="example@domain.com"
FontSize="15"
Foreground="DodgerBlue" />
<TextBlock Text="(123)456 7890"
FontStyle="Italic" />
</StackPanel>
</Grid>
3.
private Contact contact;
public Contact Contact
{
get { return contact; }
set { contact = value; }
}
4.
<Grid Background="#AAe5e5e5">
<StackPanel Margin="4">
<TextBlock Text="Name LastName"
FontSize="20"
FontWeight="Bold"
x:Name="nameTextBlock"/>
<TextBlock Text="example@domain.com"
FontSize="15"
Foreground="DodgerBlue"
x:Name="emailTextBlock"/>
<TextBlock Text="(123)456 7890"
FontStyle="Italic"
x:Name="phoneTextBlock"/>
</StackPanel>
</Grid>
nameTextBlock.Text = contact.Name;
emailTextBlock.Text = contact.Email;
phoneTextBlock.Text = contact.Phone;
02.
더보기
1.
xmlns:uc="clr-namespace:ContactApp.Control"
2.
public Contact Contact
{
get { return (Contact)GetValue(ContactProperty); }
set { SetValue(ContactProperty, value); }
}
public static readonly DependencyProperty ContactProperty = DependencyProperty.Register(
"Contact", // 의존 속성으로 등록될 프로퍼티
typeof(Contact), // 등록할 의존 프로퍼미 데이터 타입
typeof(ContactControl), // 의존 속성을 소유하게 될 Owner
new PropertyMetadata( // 의존 속성으로 등록될 프로퍼티 초기값 설정
new Contact()
{
Name = "Name LastName",
Phone = "(123)456 7890",
Email = "email@email.com"
},
SetText) // 등록된 프로퍼티 변경시 콜백
);
private static void SetText(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ContactControl control = (ContactControl)d;
if (control != null)
{
control.nameTextBlock.Text = ((Contact)e.NewValue).Name;
control.emailTextBlock.Text = ((Contact)e.NewValue).Email;
control.phoneTextBlock.Text = ((Contact)e.NewValue).Phone;
}
}
3.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<uc:ContactControl Contact="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
03.
더보기
1.
Owner = Application.Current.MainWindow;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
2.
Owner = Application.Current.MainWindow;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
04.