WPF - 如何以编程方式引用Style中的元素

时间:2011-01-14 18:14:56

标签: wpf reference element styles

如何在代码中引用ItemDecorator元素?

<Style x:Key="DesignerItemStyle" TargetType="ContentControl">
    <Setter Property="MinHeight" Value="50"/>
    <Setter Property="MinWidth" Value="50"/>
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ContentControl">
          <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
            <s:MoveThumb Cursor="SizeAll" Template="{StaticResource MoveThumbTemplate}" />
            <ContentPresenter Content="{TemplateBinding ContentControl.Content}"
                              Margin="{TemplateBinding Padding}"/>
            <s:DesignerItemDecorator x:Name="ItemDecorator"/>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="Selector.IsSelected" Value="True">
              <Setter TargetName="ItemDecorator" Property="ShowDecorator" Value="True"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

1 个答案:

答案 0 :(得分:0)

GetTemplateChild救援。有关示例,请参阅此文章:Creating a WPF Custom Control