Programing

템플릿 바인딩과 바인딩은 무엇입니까?

crosscheck 2020. 7. 30. 09:54
반응형

템플릿 바인딩과 바인딩은 무엇입니까?


이해할 수 없었습니다 BorderThickness="{TemplateBinding BorderThickness}. 여기 코드가 있습니다 :

<ControlTemplate TargetType="{x:Type wpftoolkit:DataGridCell}">
    <Border Padding="{TemplateBinding Padding}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            Background="{TemplateBinding Background}" 
            SnapsToDevicePixels="True">
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    </Border>
</ControlTemplate>

다른 유형의 바인딩도 설명하십시오.


TemplateBinding은 템플릿 정의 내의 요소 속성에 바인딩하는 데 사용됩니다. 당신의 예에서, 당신은 쓸 수있었습니다

 <Border Padding="{Binding Padding}" ...>

테두리 패딩 속성을 패딩 속성에 바인딩한다는 의미는 무엇입니까? "이 템플릿이 사용되는 컨트롤의 패딩 속성"이라고 말하고 싶습니다. 현재 컨트롤의 x : Name을 모르기 때문에 이름을 지정할 수 없습니다 (그렇더라도 이름이 다르기 때문에 작동하지 않습니다). 그러나 상대 소스를 정의하여이를 수행 할 수 있습니다.

<Border Padding="{Binding Padding, RelativeSource={RelativeSource TemplatedParent}" ...>

또는 위의 바로 가기 (*) 인 TemplateBinding을 사용하십시오.

<Border Padding="{TemplateBinding Padding}" ...>

(*) 이러한 템플릿 시나리오에서 덜 장황한 것 외에도 TemplateBinding은 일반 바인딩과 비교하여 몇 가지 차이점이 있습니다.

  • 컴파일 타임에 평가됩니다. 예를 들어 Padding 속성이 존재하지 않으면 컴파일 오류가 발생하지만 TemplatedParent와의 바인딩을 사용하는 경우 런타임시 오류 만 표시됩니다.
  • 항상 단방향 바인딩입니다.
  • 소스 및 대상 속성이 모두 종속성 속성이어야합니다 .
  • 기능이 훨씬 적습니다 (StringFormat, Delay, IsAsync 등은 없습니다. Binding vs TemplateBindingExtention 속성 참조 ).

그림은 1000 단어의 가치가 있습니다. 이 경우 비디오는 7 분입니다. https://www.youtube.com/watch?v=z-0TZR-7xLI

편집 : 예 :

  • A Button에는 기본 ControlTemplate속성과 Height속성이 있습니다
  • 직접 작성하여의 ControlTemplate속성을 재정의 합니다 Button(예 : Ellipse-looking 대신 -looking 버튼 을 만들려는 경우 Rectangle).
  • Ellipse를 만든 후에 는 원본 버튼의 속성 과 동일한 크기 ControlTemplate를 원합니다.EllipseHeight
  • 사용 그래서 TemplateBinding참조를 위해 ButtonHeight그것을 이름을 지정하지 않고여기에 이미지 설명을 입력하십시오

에렌 에르 소 멘츠 (Eren Ersönmenz)는 이미 그것을 잘 설명했지만 그 개념을 더 잘 이해할 수있는 또 다른 관점을 제시하고 싶습니다.

In WPF every control is more or less detached from its presentation. You can always change the template of controls and make it look completely different. A button works as expected with a ControlTemplate only consisting of a Rectangle for example. Now sometimes it is necessary for the ControlTemplate to actually use the properties of the logic part of a control. And thats what TemplateBinding is for it just tells the ControlTemplate "Use this property of the control we are giving the visual presentation". A good example is the Background property on every control, it has no meaning on its own, it gets its meaning by TemplateBinding it to child control in the ControlTemplate.

자체 바인딩은 MSDN 에 매우 잘 설명되어 있습니다. 이것은 실제로 내 옆에 내 벽에 걸려 아주 좋은 치트 시트입니다. 사용 가능한 모든 다른 바인딩에 대한 좋은 개요를 제공합니다.


에서 TemplateBinding 태그 확장 , TemplateBinding템플릿 화 제어에 대한 몇 가지 다른 노출 속성 값을 컨트롤 템플릿의 속성 값을 연결합니다. 즉, 템플릿에서 값을 바인딩하기위한 것입니다.

바인딩 은 바인딩 대상과 데이터 소스의 속성을 연결합니다.

참고 URL : https://stackoverflow.com/questions/10597492/what-is-the-template-binding-vs-binding

반응형