2017-02-22 51 views
1

我有以下的ControlTemplate內訪問控制:WPF,不能在控件模板

<UserControl x:Class="WpfSinergoHMIControls.Controlli.ControlButton" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:WpfSinergoHMIControls.Controlli" 
     mc:Ignorable="d"> 

<UserControl.Template> 
    <ControlTemplate TargetType="UserControl" x:Name="ControlButtonTemplate"> 
     <Grid Background="Black" Name="ControlButtonGrid"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="2*" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="1*" /> 
       <RowDefinition Height="2*" /> 
      </Grid.RowDefinitions> 

      <Label Content="{TemplateBinding Content}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="White" Margin="1,1,1,1" Grid.Row="0"></Label> 
      <Viewbox Grid.Row="1" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"> 
       <Ellipse Width="100" Height="100" Fill="White" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
      </Viewbox> 
      <Viewbox Grid.Row="1" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"> 
       <Ellipse Width="100" Height="100" Margin="0" Name="InnerEllipse" Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Viewbox> 
     </Grid> 
    </ControlTemplate> 
</UserControl.Template> 

,我想以編程方式訪問稱爲「InnerEllipse」的對象。

我嘗試用下面的代碼行這樣做:

Ellipse InnerEllipse = (Ellipse) this.Template.FindName("InnerEllipse", this); 

這就是所謂的「ControlButton」類中所謂的「顏色」屬性裏面:

public Color Color 
{ 
    set 
    { 
     Ellipse InnerEllipse = (Ellipse)this.Template.FindName("InnerEllipse", this); 
    } 
} 

財產「顏色'然後初始化當我使用'UserControl'

<Controlli:ControlButton Height="169" Width="119" Color="DarkGreen"/> 

問題是,功能「FindName」返回「null」。我找不出什麼是缺失的。非常感謝你!

+0

你試過this.FindName(「InnerEllipse」,this.Template)? – Dbl

回答

0

問題是Color屬性設置爲之前模板已被應用。

Color應該是一個依賴屬性。然後,您可以指定一個默認值以及鉤住了PropertyChangedCallback每當它改變時被調用:

public partial class ControlButton : UserControl 
{ 
    public ControlButton() 
    { 
     InitializeComponent(); 
    } 

    Ellipse InnerEllipse; 
    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     InnerEllipse = (Ellipse)this.Template.FindName("InnerEllipse", this); 
    } 

    public static readonly DependencyProperty ColorProperty = 
     DependencyProperty.Register("Color", typeof(Color), 
     typeof(ControlButton), new FrameworkPropertyMetadata(Colors.DarkGreen, new PropertyChangedCallback(OnColorChanged))); 

    public Color Color 
    { 
     get { return (Color)GetValue(ColorProperty); } 
     set { SetValue(ColorProperty, value); } 
    } 

    private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ControlButton ctrl = d as ControlButton; 
     if (ctrl.InnerEllipse != null) 
      ctrl.InnerEllipse.Fill = new SolidColorBrush() { Color = ctrl.Color }; 
    } 
} 
+0

我使用「OnApplyTemplate()」來獲取「InnerEllipse」對象,並做任何我需要做的功能。謝謝! – Enrico