J'ai un contrôle personnalisé avec un TextBlock intérieur:La liaison de TextBlock intérieur contrôle personnalisé à la dépendance propriété du même contrôle personnalisé
<Style TargetType="{x:Type local:CustControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustControl}">
<Border Background="Blue"
Height="26"
Width="26" Margin="1">
<TextBlock x:Name="PART_CustNo"
FontSize="10"
Text="{Binding Source=CustControl,Path=CustNo}"
Background="PaleGreen"
Height="24"
Width="24"
Foreground="Black">
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Et ce contrôle personnalisé a une propriété de dépendance:
public class CustControl : Control
{
static CustControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustControl), new FrameworkPropertyMetadata(typeof(CustControl)));
}
public readonly static DependencyProperty CustNoProperty = DependencyProperty.Register("CustNo", typeof(string), typeof(CustControl), new PropertyMetadata(""));
public string CustNo
{
get { return (string)GetValue(CustNoProperty); }
set { SetValue(CustNoProperty, value); }
}
}
Je souhaite que la valeur de la propriété "CustNo" soit transférée dans la propriété "Text" de TextBlock dans chaque instance du contrôle personnalisé. Mais mon:
Text="{Binding Source=CustControl,Path=CustNo}"
ne fonctionne pas.
ne fonctionne pas aussi avec Path = CustNoProperty:
Text="{Binding Source=CustControl,Path=CustNoProperty}"
ps il y a un excellent (si un peu daté) show dnrtv sur la création de contrôles personnalisés à http://www.dnrtv.com/default.aspx?showNum=72 – kiwipom
Oui, cela fonctionne. Je vous remercie! – rem