Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/692d4200-adfc-800c-833d-f176379c9e46
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Part 4: μ’ μμ± μμ± (Dependency Properties) ==== Part 4μμλ WPF νλ‘κ·Έλλ°μ ν΅μ¬ μΈνλΌ μ€ νλμΈ '''μ’ μμ± μμ±(Dependency Property)'''μ λν΄ μ€λͺ ν©λλ€stackoverflow.com<ref>{{cite web|title=stackoverflow.com|url=https://stackoverflow.com/questions/29700/can-someone-point-me-to-some-guides-for-wpf#:~:text=,into%20WPF%20Styles%20And%20Templates|publisher=stackoverflow.com|access-date=2025-12-02}}</ref>. μ’ μμ± μμ±(μ΄ν DP)μ WPFμ μμ±(Property) μμ€ν μ ꡬμ±νλ νΉλ³ν ννμ μμ±μΌλ‘, λ¨μν CLR μμ±κ³Ό λλΉλ©λλ€codeproject.com<ref>{{cite web|title=codeproject.com|url=https://www.codeproject.com/articles/WPF-A-Beginner-s-Guide-Part-4-of-n#:~:text=WPF%3A%20A%20Beginner%27s%20Guide%20,Dependency%20Properties%3B%20Dependency|publisher=codeproject.com|access-date=2025-12-02}}</ref>. WPFμ λ§μ 컨νΈλ‘€ μμ±λ€(μ: Button.Background, TextBox.Text λ±)μ΄ λͺ¨λ DPλ‘ κ΅¬νλμ΄ μμ΅λλ€. μ΄λ¬ν DPλ WPFμ λ°μ΄ν° λ°μΈλ©, μ€νμΌ, μ λλ©μ΄μ , κ° μμ λ±μ κΈ°λ₯μ κ°λ₯μΌ νλ κΈ°λ°μ λλ€. CLR μμ±κ³Ό DPμ μ°¨μ΄μ : μΌλ°μ μΈ C# ν΄λμ€ μμ±μ νλμ κ°μ μ μ₯νκ³ get/set μ κ·Όμλ₯Ό ν΅ν΄ μ½κ³ μλλ€. λ°λ©΄ WPFμ DPλ λ΄λΆμ μΌλ‘ μ μ ν΄μ ν μ΄λΈμ μμ± κ°λ€μ μ μ₯νκ³ , μμ‘΄μ± κ°μ²΄(DependencyObject)μ GetValue/SetValue λ©μλλ₯Ό ν΅ν΄ κ°μ μ½κ³ μ°λ ꡬ쑰μ λλ€. DPλ₯Ό μ¬μ©νλ©΄ λ€μκ³Ό κ°μ μ΄μ μ΄ μμ΅λλ€codeproject.com<ref>{{cite web|title=codeproject.com|url=https://www.codeproject.com/articles/WPF-A-Beginner-s-Guide-Part-4-of-n#:~:text=WPF%3A%20A%20Beginner%27s%20Guide%20,Dependency%20Properties%3B%20Dependency|publisher=codeproject.com|access-date=2025-12-02}}</ref>: * λ³κ²½ ν΅μ§ λ΄μ₯: DPλ κ° λ³κ²½ μ WPF μ체μ μλ¦Όμ μ£Όμ΄, λ°μ΄ν° λ°μΈλ©μ΄λ UI μ λ°μ΄νΈλ₯Ό μλμΌλ‘ νΈλ¦¬κ±°ν©λλ€. (μΌλ° CLR μμ±μ INotifyPropertyChanged ꡬν λ±μ μλμΌλ‘ ν΄μΌ νμ§λ§, DPλ μ΄λ° ν¨ν΄μ μμ± μμ€ν μ λ΄μ₯νκ³ μμ΅λλ€.) * μμ± κ°μ λμ κ²°μ : DP μμ€ν μ μ°μ μμκ° λ€λ₯Έ μ¬λ¬ μμ€(λ‘컬 κ°, λ°μΈλ©, μ€νμΌ, ν λ§, μ λλ©μ΄μ λ±)λ‘λΆν° μ€λ κ°λ€μ μ‘°ν©νμ¬ μ΅μ’ κ°μ κ²°μ ν©λλ€. WPF μμ§μ΄ μ΄λ¬ν μ°μ μμ λ‘μ§μ κ΄λ¦¬ν΄μ£Όλ―λ‘ κ°λ°μκ° μΌμΌμ΄ μ²λ¦¬νμ§ μμλ λ©λλ€. μλ₯Ό λ€μ΄ μ€νμΌμμ Buttonμ λ°°κ²½μ λΉ¨κ°μΌλ‘ μ νμ΄λ, μ λλ©μ΄μ μ΄ μ§νμ€μ΄λ©΄ μ λλ©μ΄μ κ°μ΄ μ°μ μ μ©λλ μμ λλ€. * κΈ°λ³Έκ°(Default)κ³Ό μμ(Inheritance): DPλ₯Ό μ μν λ λ©νλ°μ΄ν°λ‘ κΈ°λ³Έκ°μ μ§μ ν μ μμ΅λλ€. λν μμ± κ° μμμ΄λΌλ κΈ°λ₯μ ν΅ν΄, νΈλ¦¬ κ³μΈ΅ ꡬ쑰μμ λΆλͺ¨ μμμ μ€μ λ DP κ°μ΄ νΉλ³ν μμμμ κ°μ μ§μ νμ§ μμΌλ©΄ μλ μμλλλ‘ ν μ μμ΅λλ€. (μ: FontSizeλ₯Ό Windowμ μ€μ νλ©΄ λ΄λΆ λͺ¨λ ν μ€νΈ 컨νΈλ‘€λ€μ΄ κ·Έ κ°μ μμλ°μ λμΌν κΈκΌ΄ ν¬κΈ°λ₯Ό κ°μ΅λλ€.) * Attached Property μ§μ: DPλ '''μ²¨λΆ μμ±(Attached Property)'''μ ννλ‘ μ μλμ΄, μμ μ΄ μλ λ€λ₯Έ ν΄λμ€μ μμ±μ XAMLμμ μ€μ ν μ μκ² ν©λλ€. μλ₯Ό λ€μ΄ DockPanel.Dock μμ±μ DockPanelμ΄ μ μν DPμ΄μ§λ§, XAMLμμλ <Button DockPanel.Dock="Top">μ²λΌ Button νκ·Έμ λΆμ¬μ μ¬μ©ν©λλ€. μ΄λ κ² DP λ©μ»€λμ¦ λλΆμ Canvas.Left, DockPanel.Dock κ°μ λ μ΄μμ ννΈ μμ±μ μμμ μμ μμμ λΆμ¬ν μ μμ΅λλ€ (Part 1μμ λ³Έ Canvasμ DockPanelμ μ²¨λΆ μμ±λ€μ΄ λ°λ‘ κ·Έκ²μ λλ€). * μ±λ₯ λ° λ©λͺ¨λ¦¬ ν¨μ¨: μλ°± κ°μ 컨νΈλ‘€μ΄ μμ λ, DP μμ€ν μ μ€μ λ‘ κ°μ΄ λ°λ μμ±λ§ μ μ₯νκ³ κ·Έλ μ§ μμ κ²½μ° κΈ°λ³Έκ°μ 곡μ ν΄μ μ¬μ©ν©λλ€. CLR μμ±μ΄λΌλ©΄ κ° μΈμ€ν΄μ€κ° κ° νλλ₯Ό λ°λ‘ κ°μ§κ³ μμ΄μΌ νμ§λ§, DPλ νμμμλ§ κ°μ μ μ₯νλ ν¬μ μ μ₯(sparse storage) μ΅μ νλ₯Ό ν©λλ€. DPλ₯Ό μ¬μ©νλ λ°©λ²μ λ³΄ν΅ μμ‘΄μ± μμ± μλ³μλ₯Ό static νλλ‘ μ μΈνκ³ DependencyProperty.Register λ©μλλ‘ λ±λ‘νλ κ²μ λλ€. μλ₯Ό λ€μ΄ μ¬μ©μ μ μ 컨νΈλ‘€μ public string Title { get; set; } κ°μ CLR μμ±μ μ°λ λμ , λ€μκ³Ό κ°μ΄ DPλ₯Ό μ μΈν©λλ€: <syntaxhighlight lang="csharp">public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(MyControl), new PropertyMetadata("κΈ°λ³Έκ°", OnTitleChanged)); public string Title { get => (string)GetValue(TitleProperty); set => SetValue(TitleProperty, value); } </syntaxhighlight> μ μ½λμμ "Title" μ΄λ¦μΌλ‘ DPλ₯Ό λ±λ‘νκ³ , νμκ³Ό μμ μ νμ μ μ§μ νλ©°, κΈ°λ³Έκ°μ "κΈ°λ³Έκ°"μΌλ‘ μ€μ νμ΅λλ€. λν OnTitleChanged μ½λ°±μ λ£μΌλ©΄ μ΄ μμ±μ΄ λ³κ²½λ λ μλμΌλ‘ νΈμΆλμ΄ μΆκ° λ‘μ§μ μνν μ μμ΅λλ€. (μ: Titleμ΄ λ°λλ©΄ νλ©΄μ μ¦μ λ°μνκ±°λ, λ€λ₯Έ μμ±μ κ°±μ νλ λ±μ μμ μ ν μ μμ΅λλ€.) μ΄λ λ― DPλ '''μμ± λ±λ‘ μμ μ λΆκ°μ 보(Metadata)'''λ₯Ό μ§μ ν μ μμ΄, λ¨μν CLR μμ±λ³΄λ€ ν¨μ¬ νλΆν μ€μ μ΄ κ°λ₯ν©λλ€. Part 4μμλ CLR μμ±κ³Ό DPμ μ°¨μ΄λ₯Ό λ¨Όμ μ§κ³ codeproject.com<ref>{{cite web|title=codeproject.com|url=https://www.codeproject.com/articles/WPF-A-Beginner-s-Guide-Part-4-of-n#:~:text=WPF%3A%20A%20Beginner%27s%20Guide%20,Dependency%20Properties%3B%20Dependency|publisher=codeproject.com|access-date=2025-12-02}}</ref>, DPμ κ°λ κ³Ό μ¬μ©νλ λ², κ·Έλ¦¬κ³ WPF νλ μμν¬ μ체μμ DPλ₯Ό μ΄λ»κ² νμ©νλμ§ μ€λͺ ν©λλ€. νΉν WPF λ°μΈλ©κ³Ό μ€νμΌμ΄ DPλ₯Ό μ μ λ‘ λμνλ€λ μ μ κ°μ‘°ν©λλ€. μλ₯Ό λ€μ΄ TextBox.Textλ Slider.Valueλ DPλ‘ κ΅¬νλμ΄ μμ΄μ, BindingμΌλ‘ μ°κ²°νλ©΄ μμ€ κ°μ²΄μ λ³νκ° UIμ μλ λ°μλκ³ , λ°λλ‘ UIμμ μ¬μ©μκ° μμ ν κ°λ DP μμ€ν μ ν΅ν΄ μμ€μ μ λ¬λ©λλ€. λ§μ½ TextBox.Textκ° λ¨μ CLR μμ±μ΄μλ€λ©΄, μ΄λ° μλ μ°λμ μν΄ λ§€λ² μ΄λ²€νΈλ₯Ό λ£κ³ μλμΌλ‘ κ°μ λ£μ΄μ£Όλ μμ μ΄ νμνμ κ²μ λλ€. λν Part 4μμλ '''첨λΆλ μ’ μμ± μμ± (Attached DP)'''λ ν¨κ» λ€λ£¨κ³ μμ΅λλ€. λνμ μΈ μμλ‘ λ μ΄μμ κ΄λ ¨ μμ±λ€(Canvas.Left λ±)μ λ€λ©°, μ΄λ¬ν μμ±μ΄ μ¬μ€μ DPλ‘ κ΅¬νλμ΄ μμ΄ κ°λ₯νλ€λ κ²μ μ€λͺ ν©λλ€. μ²¨λΆ DPλ₯Ό λ§λ€ λλ DependencyProperty.RegisterAttachedλ₯Ό μ¬μ©νλ©°, μ£Όλ‘ XAML μμμ λΆλͺ¨-μμ κ° κ΄κ³λ₯Ό νμνκ±°λ, Behaviorλ₯Ό λΆμ¬νλ μλλ¦¬μ€ λ±μ μ°μ λλ€. λ§μ§λ§μΌλ‘, WPF κ°λ°μμ 컀μ€ν 컨νΈλ‘€μ λ§λ€ κ²½μ° μ’ μμ± μμ±μ κΌ νμ©ν΄μΌ νλ μ΄μ λ μλ΄ν©λλ€. μ¬μ©μ μ μ 컨νΈλ‘€μ μμ±μ DPλ‘ λ§λ€λ©΄, WPF λμμ΄λ(Blend) μμμ ν΄λΉ μμ±μ μ½κ² λ°μΈλ©νκ±°λ μ λλ©μ΄μ μ€ μ μκ³ , μ€ν 리보λλ μ€νμΌμμλ νμ©ν μ μμ΅λλ€. λ°λλ‘ CLR μμ±λ§ μμΌλ©΄ μ΄λ° WPFμ κ°λ ₯ν κΈ°λ₯λ€μ νμ©νκΈ° μ΄λ €μμ§λλ€. Sacha Barberλ μ΄λ³΄μλ₯Ό μν΄ λΉκ΅μ μ΄λ €μ΄ κ°λ μΈ DPλ₯Ό μμΈνκ³ λ 체κ³μ μΌλ‘ νμ΄μ£Όκ³ μμΌλ©°, μ΄λ₯Ό ν΅ν΄ λ μλ WPF μμ± μμ€ν μ λμ μ리λ₯Ό μ΄ν΄νκ³ μμ©ν μ μκ² λ©λλ€.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)