์๋
ํ์ธ์.
Page๋ฅผ ํ๋ ์์์ Navigate ํ ๋ Page ๋ด Property๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์ง๋ฌธ ๋๋ฆฝ๋๋ค.
[MyPage.xaml]
public partial class MyPage : Page
{
#region dependency property
public static readonly DependencyProperty StrProperty = DependencyProperty.Register("Str", typeof(string), typeof(MyPage), new PropertyMetadata(null, new PropertyChangedCallback(StrChangedCallback)));
#endregion
#region public property
public String Str
{
get => GetValue(StrProperty) as string;
set => SetValue(StrProperty, value);
}
#endregion
#region callback
private static void StrChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is MyPage)
{
MyPage mypage = sender as MyPage;
mypage.OnStrChanged(e.OldValue, e.NewValue);
}
}
#endregion
#region protected method
protected void OnStrChanged(object oldValue, object newValue)
{
Str = newValue as string;
}
#endregion
public MyPage()
{
InitializeComponent();
}
}
[MainWindows.xaml.cs]
MyFrame.Navigate("MyPage.xaml");
MainWindows์์ MyPage๋ฅผ ์ ์ํ๋๊ฒ ์๋๋ผ์ ํ๋กํผํฐ์ ๋ฐ์ธ๋ฉ ๊ฑฐ๋ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค
1๊ฐ์ ์ข์์
์ฒ์ ์จ๋ณธ ๊ธ์ธ๋ฐ ์ฝ๋๋ฅผ ์ฝ๋ ๋ธ๋ญ ์์ ๋ฃ๋ ๋ฒ์ ์ ํํ ๋ชจ๋ฅด๊ฒ ๋ค์.
๊ฐ๋
์ฑ ์ข์ง ์์ ์ ์ฃ์กํฉ๋๋ค.
1๊ฐ์ ์ข์์
dimohy
3
```csharp
์ฝ๋
```
์ด๋ ๊ฒ ์ฝ๋๋ฅผ ๋ฃ์ผ๋ฉด ๋์ด์.
1๊ฐ์ ์ข์์
dimohy
4
Navigate()
์ ๋๊ธธ ์ ์๋ ์ธ์๊ฐ ์์ ๊ฒ๋๋ค. Page์ ์ค๋ฒ๋ผ์ด๋ ๋ฉ์๋๊ฐ ์์๊บผ์์ OnNavigatedTo()
์ด๊ณณ์์ ๋๊ฒจ๋ฐ์ ์ธ์๋ฅผ ์ ์ฉํ์ค ์ ์์ต๋๋ค.
์ ํํ ๋ต๋ณ์ ์ํด ํ๊ฒฝ์ค๋ช
์ ์ข ๋ ํด์ฃผ์๋ฉด ์ข์ต๋๋ค.
1๊ฐ์ ์ข์์
๋ง์ํด์ฃผ์ ๋๋ก ์์ ํ์ต๋๋ค.
ํ๊ฒฐ ๋ณด๊ธฐ ํธํด์ก๋ค์.
๊ฐ์ฌํฉ๋๋ค.
2๊ฐ์ ์ข์์
๋จ์ํ ์ปจํธ๋กค์ด๋ฉด xaml์์ ํด๋น ์ปจํธ๋กค์ ํธ์ถํ๊ธฐ ๋๋ฌธ์ ์ ๋ ๋ณดํต MainWindows.xaml์์ MainWindow์ ViewModel์ ์์ฑํ๊ณ ๋ฐ์ธ๋ฉ ํฉ๋๋ค.
<Window
...
d:DataContext="{d:DesignInstance local:MainViewModel}">
<Grid>
<Button Content="{Binding Str, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
์ด๋ฐ์์ผ๋ก์
๊ทธ๋ฐ๋ฐ MainWindow์ Frame์ ์์ฑํ๊ณ ํด๋น ํ๋ ์์ ํ์ด์ง๋ฅผ Navigate() ํ ๋ ํ์ด์ง์ MainWindowViewModel์ ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉ ํ๋ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
<Window
...
d:DataContext="{d:DesignInstance local:MainViewModel}">
<Grid>
<Frame/>
</Grid>
</Window>
????? <MyPage Str="{Binding Str, UpdateSourceTrigger=PropertyChanged}"/>
``
1๊ฐ์ ์ข์์
์ ์ธ์๋ก ์ ๋๊ฒจ์ฃผ๊ณ ๋ฐ์ธ๋ฉํ๋ ค๋ ์ด์ ๋ MainPage์ Str๊ฐ์ด ๊ณ์ ๋ณํ๊ธฐ ๋๋ฌธ์
๋๋ค.
1๊ฐ์ ์ข์์
dimohy
8
WPF์ธ๊ฐ์? Navigate()
์ ๋๋ฒ์งธ ์ธ์๋ก ๊ฐ์ ์ ๋ฌํ ์ ์์ต๋๋ค. ๋ฐ์ธ๋ฉ์ด ๋ชฉ์ ์ด์๋ผ๋ฉด DataContext๋ฅผ ๋๊ฒจ์ฃผ์๋ฉด ๋ ๊บผ๊ตฌ์,
ํด๋น Page์์ NavigationService
์ LoadCompleted
์ด๋ฒคํธ๋ก ์ธ์๋ฅผ ๋ฐ์ ์ ์๊ณ ,
DataContext๋ฅผ ๋
๊ฑฐ์ฃผ๋ฉด Str
๋ฅผ ๋ฐ์ธ๋ฉํด์ ํํํ์ค ์ ์์๊บผ์์
1๊ฐ์ ์ข์์
dimohy
9
ํน์ ์ด๊ฒ๋ ๋์์ด ๋์ค ์ ์์ ๊ฒ ๊ฐ์์.
1๊ฐ์ ์ข์์
Janbyul
10
์ฝ๋๋ฅผ ์กฐ๊ธ ๋ ์ง๊ด์ ์ผ๋ก ๋ณด์ด๊ฒ ํ๊ณ ์ถ์ด์ ํ์ด์ง์ ์์ฑ์ Navigate ํ๋ ํด๋์ค์์ ๋ฐ์ธ๋ฉํ๊ณ ์ถ์ ๊ฑด๋ฐ ์ ๊ฐ ์ค๋ช
์ ๋๋ฌด ๋ชปํ๋ค์. ๋ต๋ณ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.
1๊ฐ์ ์ข์์