├── .gitignore ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 james.lee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Xaml Binding 2 | 3 | 이 리포지토리는 WPF Xaml Binding 개념과 기술을 활용하는데 필요한 설명을 다루는 Article입니다. 4 | 5 | 더 알아보기 » 6 | 7 | | Star | License | Activity | 8 | |:----:|:-------:|:--------:| 9 | | Github Stars | License | Commits-per-month | 10 | 11 |
12 | 13 | ## DataContext 14 | 15 | __DataContext는 FrameworkElement에 포함된 속성입니다.__ 16 | `PresentationFramework.dll` 17 | 18 | ```csharp 19 | namespace System.Windows 20 | { 21 | public class FrameworkElement : UIElement 22 | { 23 | public static readonly DependencyProperty DataContextProperty; 24 | public object DataContext { get; set; } 25 | } 26 | } 27 | ``` 28 | 29 | 그리고 WPF의 모든 UI 컨트롤은 `FrameworkElement` 클래스를 상속합니다. 30 | > 바인딩 또는 데이터 컨텍스트를 배워가는 시점에서 FrameworkElement를 더 깊이 연구할 필요가 없습니다. 31 | > 하지만 모든 UI 컨트롤을 포함할 수 있는 가장 가까운 개체가 FrameworkElement라는 사실을 간단히 언급하기 위함입니다. 32 |
33 | 34 | ### _DataContext is always the reference point for Binding._ 35 | Binding can directly recall values for the DataContext type format starting with the nearest DataContext. 36 | ```xaml 37 | 38 | ``` 39 | `Text="{Binding}`에 바인딩된 값은 가장 가까운 데이터 컨텍스트인 `TextBlock`에서 직접 전달됩니다. 40 | 따라서 `Text`의 바인딩 결과 값은 'James'입니다. 41 |
42 | 43 | - __Type integer__ 44 | Xaml에서 DataContext에 직접 값을 할당하는 경우 정수 및 부울과 같은 값 유형에 대해 먼저 리소스 정의가 필요합니다. 45 | 왜냐하면 모든 문자열이 문자열로 인식되기 때문입니다. 46 | 47 | #### 1. Xaml에서 System `mscrollib` 사용 48 | > Simple type variable type is not supported by standard. 49 | > 어떤 단어로도 정의할 수 있지만 대부분 `sys` 단어를 사용합니다. 50 | ```xaml 51 | xmlns:sys="clr-namespace:System;assembly=mscorlib" 52 | ``` 53 | 54 | #### 2. xaml에서 `YEAR` 리소스 키 생성 55 | > StaticResource 형식으로 생성할 유형의 값을 선언합니다. 56 | ```xaml 57 | 58 | 2020 59 | 60 | ... 61 | 62 | ``` 63 | 64 | - __All type of value__ 65 | 값이 데이터 컨텍스트에 직접 바인딩되는 경우는 거의 없습니다. 66 | Because we're going to bind an object. 67 | ```xaml 68 | 69 | true 70 | 7.77 71 | 72 | ... 73 | 74 | 75 | 76 | 77 | ``` 78 | 79 | - __Another type__ 80 | 스트링뿐만 아니라 다양한 타입이 가능합니다. 데이터 컨텍스트가 객체이기 때문입니다. 81 |
82 | 83 | WPF에서 바인딩을 사용할 때, 대부분의 개발자들은 DataContext의 기능 및 중요성에 대해 완전히 알지 못합니다. 특히 대규모 WPF 프로젝트를 담당하거나 참여하는 경우 애플리케이션의 DataContext 계층을 보다 명확하게 이해해야 합니다. DataContext 개념이 없으면 기능을 자유롭게 구현하는 데 한계가 있기 때문입니다. 84 | 85 |
86 | 87 | * * * 88 | 89 | ## Binding 90 | 91 | - [DataContext Binding](#datacontext-binding) 92 | - [Element Binding](#element-binding) 93 | - [MultiBinding](#multibinding) 94 | - [Self Property Binding](#self-property-binding) 95 | - [Find Ancestor Binding](#find-ancestor-binding) 96 | - [TemplatedParent Binding](#templatedparent-binding) 97 | - [Static Property Binding](#static-property-binding) 98 |
99 | 100 | ### DataContext Binding 101 | 102 | `string property` 103 | ```xaml 104 | 105 | ``` 106 |
107 | 108 | ### Element Binding 109 | 110 | ```xaml 111 | 112 | 113 | ``` 114 |
115 | 116 | ### MultiBinding 117 | 118 | ```xaml 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | ``` 128 |
129 | 130 | ### Self Property Binding 131 | 132 | ```xaml 133 | 134 | ``` 135 | 자신의 속성을 바인딩해야 하는 경우 요소 바인딩 대신 `Element Binding`을 사용할 수 있습니다. 136 | 자신의 속성을 바인딩하기 위해 `x:Name`을 선언할 필요가 없습니다. 137 | 138 | ```xaml 139 | 140 | ``` 141 |
142 | 143 | ### Find Ancestor Binding 144 | 145 | 가장 가까운 상위 컨트롤을 기준으로 가져옵니다. 146 | 147 | ```xaml 148 | 149 | ``` 150 |
151 | 152 | `Trigger`에서 상위 개체의 속성에 액세스할 때 유용합니다. 153 | ```xaml 154 | 155 | 156 | 157 | ``` 158 |
159 | 160 | DataContext 객체가 있는 경우 해당 속성을 사용할 수 있습니다. 161 | 그러나 `DataContext`(ViewModel)에 대한 접근은 가급적 피하는 것이 좋습니다. 162 | ```xaml 163 | 164 | ``` 165 |
166 | 167 | 현재 바인딩된 `DataContext`가 아닌 다른 `DataContext`를 바인딩하려는 경우 다음 방법을 사용할 수 있습니다. 168 | ```csharp 169 | public partial class A : UserControl 170 | { 171 | public A() 172 | { 173 | InitializeComponent(); 174 | DataContext = new MainViewModel(); 175 | } 176 | 177 | public class MainViewModel 178 | { 179 | public B G1VM { get; set; } = new B(); 180 | public C G2VM { get; set; } = new C(); 181 | } 182 | } 183 | ``` 184 | 185 | ```xaml 186 | 187 | 188 | 190 | 191 | 193 | 194 | 195 | ``` 196 |
197 | 198 | ### TemplatedParent Binding 199 | 200 | `ControlTemplate` 내에서 사용할 수 있는 메서드로 `ControlTemplate`의 자신의 속성을 가져올 수 있습니다. 201 | 202 | ```xaml 203 | 212 | ``` 213 | 214 | 모든 속성 및 데이터 컨텍스트에 접근할 수 있습니다. 215 | 216 | ```xaml 217 | 218 | ``` 219 |
220 | 221 | ### Static Property Binding 222 | 223 | 바인딩 속성 값에 직접 접근할 수 있습니다. 224 | 225 | #### 1. `static` 속성을 선언합니다. 226 | 227 | ```csharp 228 | namespace Exam 229 | { 230 | public class ExamClass 231 | { 232 | public static string ExamText { get; set; } 233 | } 234 | } 235 | ``` 236 | 237 | #### 2. XAML에서 정적 클래스를 사용합니다. 238 | 239 | ```xaml 240 | 241 | ``` 242 | 243 | #### 3. Binding property. 244 | 245 | ```xaml 246 | 247 | ``` 248 | 249 | _또는 `Converter`를 사용하는 것처럼 리소스 키를 설정할 수 있습니다._ 250 | 251 | ```xaml 252 | 253 | 254 | 255 | 256 | ... 257 | 258 | 259 | ``` 260 | 261 |
262 | 263 | * * * 264 | 265 | ## Bad Binding & Good Binding 266 | 267 | #### :heavy_check_mark: 바인딩할 속성이 DataContext에 포함된 경우 ElementBinding을 사용할 필요가 없습니다. 268 | DataContext에 포함된 속성에 해대해 ElementBinding을 사용하는 것은 기능적으로 문제가 없지만, 바인딩의 기본 패턴을 깰 수 있습니다. 269 | 270 | #### :slightly_frowning_face: Bad Binding 271 | 272 | ```xaml 273 | 274 | ... 275 | 276 | ``` 277 | 278 | #### :grinning: Good Binding 279 | 280 | ```xaml 281 | 282 | ... 283 | 284 | ``` 285 | 286 |
287 | 288 | #### :heavy_check_mark: 상위 컨트롤의 속성을 사용할 때는 ElementBinding을 사용하지 마세요. 289 | 290 | #### :slightly_frowning_face: Bad Binding 291 | 292 | ```xaml 293 | 294 | 295 | ... 296 | ``` 297 | 298 | #### :grinning: Good Binding 299 | 300 | ```xaml 301 | 302 | 303 | ... 304 | ``` 305 | 306 | #### :laughing: Great! 307 | 308 | ```xaml 309 | 310 | 312 | ... 313 | ``` 314 |
315 | 316 | #### :heavy_check_mark: 자기 자신의 속성을 사용할 때 ElementBinding을 사용하지 마세요. 317 | 318 | #### :slightly_frowning_face: Bad Binding 319 | 320 | ```xaml 321 | 322 | ``` 323 | 324 | #### :grinning: Good Binding 325 | 326 | ```xaml 327 | 328 | ``` 329 | 330 |
331 | 332 | ## Reference 333 | [:bookmark_tabs:](https://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource) **StackOverflow**   How do I use WPF bindings with RelativeSource? 334 | --------------------------------------------------------------------------------