Je le ControlTemplate suivant:WPF, ne peut pas accéder à un contrôle dans un ControlTemplate
<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>
et je voudrais accéder à l'objet appelé programme « InnerEllipse ».
J'essaie de le faire avec la ligne de code suivante:
Ellipse InnerEllipse = (Ellipse) this.Template.FindName("InnerEllipse", this);
qui est appelée dans une propriété appelée « Couleur » dans la catégorie « ControlButton »:
public Color Color
{
set
{
Ellipse InnerEllipse = (Ellipse)this.Template.FindName("InnerEllipse", this);
}
}
la propriété « Couleur 'est alors initializated quand j'utilise le « UserControl »
<Controlli:ControlButton Height="169" Width="119" Color="DarkGreen"/>
le problème est que la fonction "FindName" me renvoie "null". Je ne peux pas comprendre ce qui manque. Merci beaucoup!
Avez-vous essayé this.FindName (« InnerEllipse », this.Template)? – Dbl