2009-08-29 1 views
15

나는 이런 식으로 뭔가를 할 노력하고있어 ...내 WPF 스타일 설정 기가 TemplateBinding을 사용할 수 있습니까?

<Style 
    x:Key="MyBorderStyle" 
    TargetType="Border"> 
    <Setter 
     Property="BorderBrush" 
     Value="{StaticResource MyBorderBrush}" /> 
    <Setter 
     Property="Background" 
     Value="{StaticResource MyBackgroundBrush}" /> 
    <Setter 
     Property="Padding" 
     Value="{TemplateBinding Padding}" /> 
</Style> 

...하지만 오류 얻을 : 나는 "자격 유형 이름"을 제공하려면 어떻게 'Padding' member is not valid because it does not have a qualifying type name.

를?

참고 :이 작업을 수행하려는 이유는 동일한 테두리를 일련의 유사한 ControlTemplates에 포함시키고 싶기 때문입니다.

감사합니다.

편집 : 그럼 난이 시도

...

<Setter 
    Property="Padding" 
    Value="{TemplateBinding GridViewColumnHeader.Padding}" /> 

... 그리고 실제로 컴파일,하지만 내가 응용 프로그램을 실행했을 때, 나는 XamlParseException 가지고 :

Cannot convert the value in attribute 'Value' to object of type ''.

PaddingGridViewColumnHeader (이 스타일을 사용하려는 ControlTemplate)은 작동하지만 주사위는 사용하지 않습니다.

편집 2 :

음, TemplateBinding의 문서에 따르면, 말한다 : 내가 할 노력하고있어 것은 그냥 일반 불가능처럼

Links the value of a property in a control template to be the value of some other exposed property on the templated control.

그래서 소리가 난다. 난 정말 내 컨트롤 템플릿에서 특정 컨트롤에 대한 재사용 가능한 스타일을 만들 수 싶습니다,하지만 템플릿 바인딩은 이러한 스타일에 포함되지 않을 것 같아요.

답변

31

컨트롤을 템플릿으로 만들고 해당 컨트롤의 속성 값을 템플릿 내의 다른 컨트롤의 속성에 바인딩하려는 경우에 적합합니다. 귀하의 경우 템플리트를 만들고 (MyControl이라고 함) 템플리트에는 Padding이 MyControl의 패딩에 바인딩되어야하는 테두리가 포함됩니다. MSDN documentation에서

: 어떤 이유로

A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}}.

, 바인딩의 소스로 TemplatedParent를 지정은 스타일 세터 내에서 작동하지 않는 것 같습니다. 이 문제를 해결하기 위해 상대 부모를 템플릿으로 사용하는 컨트롤의 AncestorType으로 지정할 수 있습니다. MyControl 템플릿에 다른 MyControl을 포함시키지 않으면 TemplatedParent를 효과적으로 찾습니다.

단추의 ControlTemplate에있는 TextBlock의 Text 속성에 Button의 (String) 내용을 바인딩해야하는 Button 컨트롤을 사용자 지정 템플릿에 사용할 때이 솔루션을 사용했습니다.이 코드는 다음과 같습니다.

<StackPanel> 
    <StackPanel.Resources> 
     <ControlTemplate x:Key="BarButton" TargetType="{x:Type Button}"> 
      <ControlTemplate.Resources> 
       <Style TargetType="TextBlock" x:Key="ButtonLabel"> 
        <Setter Property="Text" Value="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type Button}} }" /> 
       </Style> 
      </ControlTemplate.Resources> 
      <Grid> 
       <!-- Other controls here --> 
       <TextBlock Name="LabelText" Style="{StaticResource ButtonLabel}" /> 
      </Grid> 
     </ControlTemplate> 
    </StackPanel.Resources> 
    <Button Width="100" Content="Label Text Here" Template="{StaticResource BarButton}" /> 
</StackPanel> 
+0

좋은 생각, 셰인. 감사. – devuxer

3

속성 이름 앞에 접두어를 붙이면 속성을 쉽게 확인할 수 있습니다. 예를 들어, Padding 대신 Border.Padding입니다.

그러나 귀하의 시나리오에 맞는 것이 확실하지 않습니다. TemplateBinding은 제어 템플릿 내에서 사용됩니다.

+1

감사합니다. 당신의 대답은 나에게 시도해 볼 아이디어를 주었지만 (위의 편집 참조) 작동하지 않았습니다. 'TemplateBinding'은 ControlTemplate 내에서만 사용될 수 있습니다 ... 만약 내가 ControlTemplate 내에서만이 스타일을 사용하려고한다는 것을 파서에게 확신시킬 수 있다면 ... – devuxer

관련 문제