์๋
ํ์ธ์. 
WPF DataTemplate ์์ญ์ ์ ์ฉ์ด ๋๋๋ฐ Binding์ด ๋์ง ์์ต๋๋ค.
์ด๋ค ๋ฌธ์ ๊ฐ ์์๊น์?
๊ฐ๋จํ๊ฒ ๋๋ ๊ฒ์ผ๋ก ์๊ณ ์์๋๋ฐ..
ContentTemplate ์์์ Binding์ ์ธ์ํ์ง ๋ชปํ๋ค์.
์๋ ์์ค๋ ๊ฐ์ํ ์ํจ ๋ถ๋ถ์
๋๋ค.
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Padding="10" Background="#eeeeee">
<StackPanel>
<TextBlock Text="Original"/>
<TextBlock Text="{Binding}"/>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
DataContext="{Binding}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Padding="10" Background="#aaaaaa" Margin="10">
<StackPanel>
<TextBlock Text="Custom"/>
<TextBlock Text="{Binding}"/>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

ํน์ ์ ๊ฐ ๋นผ๋จน๊ฑฐ๋ ์๋ชป ํ ๋ถ๋ถ์ด ์์๊น์?
์ฝ์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. 
1๊ฐ์ ์ข์์
์ฌ๋ ค์ฃผ์ xaml์ฝ๋์์ผ๋ก๋ ๋ฌธ์ ๋ ์์ด ๋ณด์
๋๋ค.
๋ฐ์ธ๋ฉ ๊ฒฝ๋ก๊ฐ ์๋ชป ๋์ด ๋ด๋ถ์ ์ผ๋ก ์ค๋ฅ๊ฐ ๋์ ํ์๊ฐ ์๋๊ณ ์๋ ๊ฒ์ผ ์ ์์ต๋๋ค.
์ค์ ์ฌ์ฉ๋๋ Button์ Content์์ญ๊ณผ ํด๋น ๋ฐ์ธ๋ฉ ๋์์ ViewModel๋ถ๋ถ๋ ๊ฐ์ด ๋ด์ผ ๋ฌธ์ ์ ์ ํ์
ํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
VS์์ [์ถ๋ ฅ]์ฐฝ์ ๋ฐ์ธ๋ฉ ๊ด๋ จ ์ค๋ฅ๊ฐ ์ฐํ ์์ง ์๋์ง ๋ค์ํ๋ฒ ํ์ธํด ๋ณด์ธ์!
- ์ฐธ๊ณ : ContentPresenter์์ ๋ฐ์ธ๋ฉ ์์ญ์ด ๋ค๋ฅธ ViewModel์ด ์๋๋ผ๋ฉด DataContext๋ ์๋ตํ์
๋ ๋ฉ๋๋ค.
2๊ฐ์ ์ข์์
@aroooong ์ ๊ฐ ์ค๋ช
์ด ๋ถ์กฑํ๋ค์. ์ฃ์กํฉ๋๋ค. 
๋ง์ํด์ฃผ์ ๊ฒ ์ฒ๋ผ ์ ๊ฐ ์๊ฐํ๊ธฐ์๋ ๋ฌธ์ ๊ฐ ์์ด ๋ณด์์ง๋งโฆ
ContentTemplate๊น์ง DataContext๊ฐ ๋๋ฌํ์ง ์๊ณ ์์ต๋๋ค.
GitHub์ ํ
์คํธํ ์ํ ์์ค์ฝ๋๋ ์ฌ๋ ค๋๊ฒ ์ต๋๋ค!
๊ทธ๋ฆฌ๊ณ ์ ๊ฐ ์คํ์์ค๋ก ๋ง๋ค๊ณ ์๋ ํ๋ก์ ํธ(DevFlow)๋ ๋๊ฐ์ ์ฆ์์
๋๋ค.
์ ๊ฒฝ์จ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! 
MainWindow.xaml
<Window x:Class="WpfApp6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp6"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button/>
</Grid>
</Window>
App.xaml
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Padding="10" Background="#eeeeee">
<StackPanel>
<TextBlock Text="Original"/>
<TextBlock Text="{Binding}"/>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
DataContext="{Binding}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Padding="10" Background="#aaaaaa" Margin="10">
<StackPanel>
<TextBlock Text="Custom"/>
<TextBlock Text="{Binding}"/>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
MainViewModel.cs
public class MainViewModel
{
}
์., ์ ๊ฐ ๋ฌธ์ ๋ฅผ ์ ํํ ํ์
ํ์ง ๋ชปํ๊ฑธ๊น์โฆ?
์ฌ๋ ค์ฃผ์ MainViewModel์๋ ๋น์ด ์๊ณ
์ค์ ์ฌ์ฉ๋๋ Button์๋ Content์์ฑ์ด ๋น์ด ์๋ ์ํ์์
ContentTemplate์์ญ์
TextBlock Text=โ{Binding}โ <-๋ ๊ฐ์ด ๋น์ฐํ ๋น์ด ์๋๊ฒ ์ ์ ์๋๊ฐ์??
๋ค์๊ณผ ๊ฐ์ด ๋ฐ๊ฟ๋ณด๋ฉด ์ ์ ๋ฐ์ธ๋ฉ ๋ฉ๋๋ค.
[MainViewModel.cs]
![image]()
[MainWindow.xaml]
![image]()
[๊ฒฐ๊ณผ]

2๊ฐ์ ์ข์์
ํน์ฌ๋โฆ ์ฐธ๊ณ ๋ก
ControlTemplate ์์ญ์ Binding์
์๊ธฐ ์์ ์ DataContext์ ๊ฐ์ฒด๊ฐ ๋ด๋ถ์ ์ผ๋ก ToString๋์ ํ ๋ค์์คํ์ด์ค ๊ฒฝ๋ก๊ฐ ์ฐํ ๊ฒ์ด๊ณ
ContentTemplate์์ญ์ Binding์
๋ฒํผ์ Content์์ญ์ ์ง์ ๋ ๊ฐ์ ๋ฐ๋ผ๋ณด๊ธฐ์โฆ ํด๋น Content๋ ์ง์ ๋์ง ์์ ์๋ฌด ํ์๊ฐ ๋์ง ์๋ ๊ฒ ์
๋๋ค.
1๊ฐ์ ์ข์์
@aroooong ์ ํํ ๋ต๋ณ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. 
์ค๋ช
์ฃผ์ ๋๋ก ContentPresenter๋ ๊ธฐ๋ณธ์ ์ผ๋ก Content๊ฐ ์ฐ๊ฒฐ๋์ด ์๋๋ฐ
๋น์ฐํ ๋์ค์ง ์๋ ๊ฒ์ ์ ๊ฐ ์๋ฑํ๊ฒ ์๋ํด๋ณด๊ณ ์์๋ค์. 
ContentPresenter์ ์๋ DataTemplate์ DataContext์ ๋ฌด๊ดํ๋ค๋ ๊ฒ๋ ์๊ฒ ๋์์ต๋๋ค.
๋ง์ํด์ฃผ์ ๋ถ๋ถ ์ดํดํ๊ณ ์ด๋ ๊ฒ ์ฒ๋ฆฌํ์ต๋๋ค.
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{Binding}"/>
๊ทธ๋ฆฌ๊ณ ContentSource๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๊ฒ ๋์์ต๋๋ค.
(๊ธฐ๋ณธ ๊ฐ์ด Content์ธ ๊ฒ ๊ฐ์ต๋๋ค.)
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
ContentSource="DataContext"/>
@aroooong ์ ์ค๋ช
ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.!