2015-05-13 28 views
1

我想獲取作爲我的內容控件的模板的元素(路徑)。我想獲得有關「contour_forme」的參考,但在互聯網上找到的每個解決方案均返回null。我試圖重寫OnApplyTemplate但仍然無效:WPF從代碼中的模板中獲取元素

public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     var template = this.pion_test.Template; 
     var contour = GetTemplateChild("contour_forme"); 
     forme_path = template.FindName("contour_forme", this.pion_test) as Path; 
     Path path = FindVisualChild<Path>(this.pion_test); 
     var test = this.pion_test.FindName("contour_forme"); 
     var test2 = this.pion_test.Template.FindName("contour_forme", this.pion_test); 
    } 

我的XAML:

<UserControl.Resources> 
     <Style TargetType="{x:Type ContentControl}" x:Key="StylePion"> 
      <Setter Property="BorderBrush" Value="Black"/> 
      <Setter Property="Background" Value="White" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ContentControl"> 
         <Grid Width="33" Height="34"> 
          <Path x:Name="contour_forme" 
           Stroke="{TemplateBinding BorderBrush}" 
           StrokeThickness="1" 
           Stretch="Uniform" 
           Width="28" 

           HorizontalAlignment="Left" 
            Margin="0,-1,0,0" 
            RenderTransformOrigin="0.59,0.5" 
           Fill="{TemplateBinding Background}" 
           Data="M17.36,24.623c-2.142-0.006-4.153-0.552-5.907-1.506l0,0l-0.076-0.046 
     c-1.952-1.08-3.57-2.673-4.692-4.589L0,11.942l7.126-6.514c1.077-1.584,2.519-2.896,4.2-3.84l0.127-0.077l0,0 
     C13.23,0.544,15.27-0.006,17.441,0c6.844z" 
            Tag="{TemplateBinding Tag}" 
           > 
           <Path.RenderTransform> 
            <RotateTransform Angle="50"/> 
           </Path.RenderTransform> 
          </Path> 
          <Ellipse Width="21" Height="21" VerticalAlignment="Center" 
            HorizontalAlignment="Center" 
           Fill="{TemplateBinding Background}" 
            /> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </UserControl.Resources> 
    <Grid Name="grid_pion"> 
     <ContentControl Name="pion_test" HorizontalAlignment="Center" VerticalAlignment="Center" Tag="4" Content="5" Style="{StaticResource StylePion}" > 

     </ContentControl> 
</Grid> 

我想找到一個基準來設定就可以了旋轉變換。

我在做什麼錯了?

回答

2

這似乎OnApplyTemplateUserControlContentControl

你可以寫這樣的自定義控件:

public class MyControl : ContentControl 
{ 
    public MyControl() 
    { 
     DefaultStyleKey = typeof(MyControl); 
    } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     var template = this.pion_test.Template; 
     var contour = GetTemplateChild("contour_forme"); 
     forme_path = template.FindName("contour_forme", this.pion_test) as Path; 
     Path path = FindVisualChild<Path>(this.pion_test); 
     var test = this.pion_test.FindName("contour_forme"); 
     var test2 = this.pion_test.Template.FindName("contour_forme", this.pion_test); 
    } 
} 

而且模板:

<Style TargetType="{x:Type MyControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="MyControl"> 
      ..... 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

似乎現在的工作,謝謝! – user2088807