[WPF] Page Property Binding

์•ˆ๋…•ํ•˜์„ธ์š”.

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

```csharp
์ฝ”๋“œ
```

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋„ฃ์œผ๋ฉด ๋˜์–ด์š”.

์ข‹์•„์š” 1

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

WPF์ธ๊ฐ€์š”? Navigate()์˜ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”์ธ๋”ฉ์ด ๋ชฉ์ ์ด์‹œ๋ผ๋ฉด DataContext๋ฅผ ๋„˜๊ฒจ์ฃผ์‹œ๋ฉด ๋  ๊บผ๊ตฌ์š”,

ํ•ด๋‹น Page์—์„œ NavigationService์˜ LoadCompleted์ด๋ฒคํŠธ๋กœ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ ,
DataContext๋ฅผ ๋…๊ฑฐ์ฃผ๋ฉด Str๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์„œ ํ‘œํ˜„ํ•˜์‹ค ์ˆ˜ ์žˆ์„๊บผ์—์š”

์ข‹์•„์š” 1

ํ˜น์‹œ ์ด๊ฒƒ๋„ ๋„์›€์ด ๋˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.

์ข‹์•„์š” 1

์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ๋” ์ง๊ด€์ ์œผ๋กœ ๋ณด์ด๊ฒŒ ํ•˜๊ณ  ์‹ถ์–ด์„œ ํŽ˜์ด์ง€์˜ ์†์„ฑ์„ Navigate ํ•˜๋Š” ํด๋ž˜์Šค์—์„œ ๋ฐ”์ธ๋”ฉํ•˜๊ณ  ์‹ถ์€ ๊ฑด๋ฐ ์ œ๊ฐ€ ์„ค๋ช…์„ ๋„ˆ๋ฌด ๋ชปํ–ˆ๋„ค์š”. ๋‹ต๋ณ€์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์ข‹์•„์š” 1