Comment puis-je utiliser une liaison ElementName dans un ControlTemplate?
J'ai plusieurs TextBlocks qui référence les différents éléments dans mon application. Mon code fonctionne très bien lorsqu'elle est utilisée directement dans la page. Cependant, je veux créer un ControlTemplate et un ContentControl pour réduire la duplication de code.
Comment puis-je passer une référence à un ElementName dans le ControlTemplate de la ContentControl utilisant objet templatebinding? Le code suivant génère cette erreur:
"Ne peut pas convertir la valeur dans l'attribut "ElementName" à un objet de type
'Système.String". Objet de type
'Système.De Windows.TemplateBindingExpression " ne peuvent pas être convertis en type
'Système.String". "
En plus de l'attribut de Balise, j'ai essayé ContentStringFormat qui n'a pas de travail.
Quelle est la bonne méthode pour obtenir que cela fonctionne à l'aide de modèles?
Merci d'avance pour votre aide,
--- Shawn
Voici l'exemple de code:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Page.Resources>
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
<TextBlock Margin="{Binding ElementName={TemplateBinding Tag}, Path=Margin}" Text="{TemplateBinding Content}" TextAlignment="{Binding ElementName={TemplateBinding Tag}, Path=TextAlignment}" Width="{Binding ElementName={TemplateBinding Tag}, Path=Width}" />
</ControlTemplate>
</Page.Resources>
<StackPanel>
<TextBlock x:Name="AnotherElement" Margin="10" Text="Main TextBlock" TextAlignment="Center" Width="100" />
<TextBlock x:Name="AnotherElement2" Margin="20" Text="Second TextBlock" TextAlignment="Left" Width="250" />
<TextBlock Margin="{Binding ElementName=AnotherElement, Path=Margin}" Text="Here is my TextBlock!" TextAlignment="{Binding ElementName=AnotherElement, Path=TextAlignment}" TextTrimming="CharacterEllipsis" Width="{Binding ElementName=AnotherElement, Path=Width}" />
<TextBlock Margin="{Binding ElementName=AnotherElement2, Path=Margin}" Text="Here is my Second TextBlock!" TextAlignment="{Binding ElementName=AnotherElement2, Path=TextAlignment}" TextTrimming="CharacterEllipsis" Width="{Binding ElementName=AnotherElement2, Path=Width}" />
<ContentControl Content="Hello!" Tag="AnotherElement" Template="{StaticResource MyTemplate}" />
<ContentControl Content="Hello Again!" Tag="AnotherElement2" Template="{StaticResource MyTemplate}" />
</StackPanel>
</Page>
source d'informationauteur Shawn Roser
Vous devez vous connecter pour publier un commentaire.
Cela semble être une drôle de manière de modèle de quelque chose, mais il peut être fait, vous avez juste à obtenir un peu de fantaisie avec de vos fixations.
L'-dessous fonctionne, mais je ne pense pas que c'est un bon moyen pour modèle un contrôle
Lier la
TextBlock
Tag
à l'Élément actuel, puis dans leControlTemplate
lierTag
àTag
et utiliser les valeurs à partir de là que la Balise est l'Élément, vous pouvez utiliser n'importe quel élément.