RelativeSource.TemplatedParent 属性wpf


今天看到这一句代码时候,自己只是知道绑定了,可是不知道绑定了什么啊 
就去查了一下,后来说的好像是绑定的TemplateParent返回的 一个值。可是这是为什么呢, 
有的说是绑定的是一个资源。 
下面有一个例子 
下面的示例显示名为 NumericUpDown 的自定义控件的 Style 定义。将 TextBlock 的 Text 属性绑定到对象 TemplatedParent 的 Value,在此情况下即应用了此 Style 的 NumericUpDown 控件。

<!--ControlTemplate for NumericUpDown that inherits from
    Control.-->
<Style TargetType="{x:Type local:NumericUpDown}">
  <Setter Property="HorizontalAlignment" Value="Center"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
        <Grid Margin="3">
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>

          <Border BorderThickness="1" BorderBrush="Gray" 
                  Margin="2" Grid.RowSpan="2" 
                  VerticalAlignment="Center" HorizontalAlignment="Stretch">

            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" 
                       Width="60" TextAlignment="Right" Padding="5"/>
          </Border>

          <RepeatButton Command="{x:Static local:NumericUpDown.IncreaseCommand}"  
                        Grid.Column="1" Grid.Row="0">Up</RepeatButton>

          <RepeatButton Command="{x:Static local:NumericUpDown.DecreaseCommand}"
                        Grid.Column="1" Grid.Row="1">Down</RepeatButton>

        </Grid>

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

TemplateBindingExtension 类 
实现一个标记扩展,该标记扩展支持在模板中属性的值和模板化控件上某个其他公开的属性的值之间进行绑定。 
下面的示例演示 ControlTemplate,它定义一个水平放置且具有圆角的 ListBox。TemplateBinding 指定 Border 的 Background 应与 ListBox 上设置的 Background 值同步。如果想要使控件用户能够控制某些属性的值,可以在 ControlTemplate 中使用 TemplateBinding

C#

<Style TargetType="ListBox">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBox">
        <Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
          <ScrollViewer HorizontalScrollBarVisibility="Auto">
            <StackPanel Orientation="Horizontal"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       IsItemsHost="True"/>
          </ScrollViewer>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

下面的示例演示 Label 控件的 ControlTemplate。HorizontalAlignment 和 VerticalAlignment 属性绑定到此 ControlTemplate 所应用于的 Label 控件的 HorizontalContentAlignment 和 VerticalContentAlignment 属性的值。

C#

<Style x:Key="{x:Type Label}" TargetType="Label">
  <Setter Property="HorizontalContentAlignment" Value="Left"/>
  <Setter Property="VerticalContentAlignment" Value="Top"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Label">
        <Border>
          <ContentPresenter 
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
            RecognizesAccessKey="True"/>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground"
                    Value="{StaticResource DisabledForegroundBrush}"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
原文地址:https://www.cnblogs.com/sjqq/p/8454513.html