locked
System.InvalidCastException lors d'un binding sur des parametres d'un usercontrol RRS feed

  • Question

  • Bonjour,

    J'essaye de binder des données sur les paramêtres de mon usercontrol mais impossible,

    Exception : Exception levée : 'System.InvalidCastException' dans .exe ("Unable to cast object of type 'Windows.UI.Xaml.Data.Binding' to type 'System.String'."). 

    mon usercontrol

        <Grid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0">
                <Ellipse Stroke="#00000000" VerticalAlignment="Center" Width="56" Height="56" d:LayoutOverrides="LeftPosition, RightPosition, TopPosition, BottomPosition" Margin="0">
                    <Ellipse.Fill >
                        <ImageBrush Stretch="UniformToFill" ImageSource="{Binding AuthorCover, Mode=OneWay}"/>
                    </Ellipse.Fill>
                </Ellipse>
                <Grid Margin="5,0">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" d:LayoutOverrides="Width" VerticalAlignment="Bottom">
                        <TextBlock Text="{Binding Path=AuthorUsername, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource AppBackground}" d:LayoutOverrides="LeftPosition, RightPosition" HorizontalAlignment="Left"/>
                        <TextBlock Text=" a été d'humeur " FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" HorizontalAlignment="Left"/>
                        <TextBlock Text="{Binding MoodName, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource AppBackground}" HorizontalAlignment="Left"/>
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Top">
                        <TextBlock  Text=" et a aimé " FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" d:LayoutOverrides="LeftPosition, RightPosition" HorizontalAlignment="Left"/>
                        <TextBlock  Text="{Binding Count, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" HorizontalAlignment="Left"/>
                        <TextBlock  Text=" " FontSize="{StaticResource FontSizeTextHome}" HorizontalAlignment="Left" FontFamily="{StaticResource FontFamilyLogin}"/>
                        <TextBlock  Text="{Binding ContentTypeName, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" HorizontalAlignment="Left"/>
                    </StackPanel>
                </Grid>
            </StackPanel>
            <TextBlock Text="{Binding Time, Mode=TwoWay}" HorizontalAlignment="Right" Width="33" VerticalAlignment="Center" Margin="0" Foreground="{StaticResource BrushGrey}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" />
    
        </Grid>
    
        public sealed partial class UpPostControl : UserControl, INotifyPropertyChanged
        {
            public string AuthorCover
            {
                get { return (string)GetValue(AuthorCoverProperty); }
                set { SetValueDp(AuthorCoverProperty, value); }
            }
            public string AuthorUsername
            {
                get { return (string)GetValue(AuthorUsernameProperty); }
                set { SetValueDp(AuthorUsernameProperty, value); }
            }
            public string MoodName
            {
                get { return (string)GetValue(MoodNameProperty); }
                set { SetValueDp(MoodNameProperty, value); }
            }
            public string Count
            {
                get { return (string)GetValue(CountProperty); }
                set { SetValueDp(CountProperty, value); }
            }
            public string ContentTypeName
            {
                get { return (string)GetValue(ContentTypeNameProperty); }
                set { SetValueDp(ContentTypeNameProperty, value); }
            }
            public string Time
            {
                get { return (string)GetValue(TimeProperty); }
                set { SetValueDp(TimeProperty, value); }
            }
    
            public static readonly DependencyProperty AuthorCoverProperty = DependencyProperty.Register
            (
                 "Cover",
                 typeof(string),
                 typeof(UpPostControl),
                 null
            );
            public static readonly DependencyProperty AuthorUsernameProperty = DependencyProperty.Register
            (
                 "Username",
                 typeof(string),
                 typeof(UpPostControl),
                 null
            );
    
            public static readonly DependencyProperty MoodNameProperty = DependencyProperty.Register
            (
                 "MoodName",
                 typeof(string),
                 typeof(UpPostControl),
                 null
            );
    
            public static readonly DependencyProperty CountProperty = DependencyProperty.Register
            (
                 "Count",
                 typeof(string),
                 typeof(UpPostControl),
                 null
            );
    
            public static readonly DependencyProperty ContentTypeNameProperty = DependencyProperty.Register
            (
                 "ContentType",
                 typeof(string),
                 typeof(UpPostControl),
                 null
            );
    
            public static readonly DependencyProperty TimeProperty = DependencyProperty.Register
            (
                 "Time",
                 typeof(string),
                 typeof(UpPostControl),
                 null
            );
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void SetValueDp(DependencyProperty property, object value, [System.Runtime.CompilerServices.CallerMemberName] string p = null)
            {
                SetValue(property, value);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(p));
            }
    
            public UpPostControl()
            {
                this.InitializeComponent();
                (this.Content as FrameworkElement).DataContext = this;
             }
        }
    

    Mon datatemplate dans lequel est utilisé mon usercontrol

                    <Grid x:Name="UP">
                        <!--<sharecontrols:UpPostControl AuthorCover="https://upload.wikimedia.org/wikipedia/commons/7/71/4416x3312-clouds-nature-sky-IMG_7627.jpg" AuthorUsername="{Binding Author.Username,Mode=OneWay}" ContentTypeName="Musiques" Count="3" MoodName="Happy" Time="5" />-->
                        <sharecontrols:UpPostControl AuthorUsername="{Binding Author.Username}" />
                    </Grid> 
    // le reste du fichier est pas important

    le datatemplate "marche" le bindings fonctionnent.

    Du coup lorsque j'utilise le binding dans le datatemplate pour completer les parametres du  usercontrol j'ai l'exception qui se lève en revanche si je mets les donées en "dur" tout marche...

    Cordialement,

    lundi 25 avril 2016 14:43

Toutes les réponses

  • Bonjour, magicthoto_bx,

    Avez-vous complété les démarches décrites dans cet article? : 
    FrameworkElement.DataContext Property
    Pouvez-vous publier le contenu de la balise <UserControl></UserControl>? 
    Je vous remercie par avance de votre retour.

    Cordialement,
    Teodora


    Votez! Appel à la contribution TechNet Community Support. LE CONTENU EST FOURNI "TEL QUEL" SANS GARANTIE D'AUCUNE SORTE, EXPLICITE OU IMPLICITE. S'il vous plaît n'oubliez pas de "Marquer comme réponse" les réponses qui ont résolu votre problème. C'est une voie commune pour reconnaître ceux qui vous ont aidé, et rend plus facile pour les autres visiteurs de trouver plus tard la résolution.

    mardi 26 avril 2016 10:59
  • Bonjour,

    Pour ce qui est de <UserControl></UserControl>:

    <UserControl
        x:Class="xxx.Shared.UpPostControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:xxx.Shared"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0">
                <Ellipse Stroke="#00000000" VerticalAlignment="Center" Width="56" Height="56" d:LayoutOverrides="LeftPosition, RightPosition, TopPosition, BottomPosition" Margin="0">
                    <Ellipse.Fill >
                        <ImageBrush Stretch="UniformToFill" ImageSource="{Binding AuthorCover, Mode=OneWay}"/>
                    </Ellipse.Fill>
                </Ellipse>
                <Grid Margin="5,0">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" d:LayoutOverrides="Width" VerticalAlignment="Bottom">
                        <TextBlock Text="{Binding Path=AuthorUsername, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource AppBackground}" d:LayoutOverrides="LeftPosition, RightPosition" HorizontalAlignment="Left"/>
                        <TextBlock Text=" a été d'humeur " FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" HorizontalAlignment="Left"/>
                        <TextBlock Text="{Binding MoodName, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource AppBackground}" HorizontalAlignment="Left"/>
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Top">
                        <TextBlock  Text=" et a aimé " FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" d:LayoutOverrides="LeftPosition, RightPosition" HorizontalAlignment="Left"/>
                        <TextBlock  Text="{Binding Count, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" HorizontalAlignment="Left"/>
                        <TextBlock  Text=" " FontSize="{StaticResource FontSizeTextHome}" HorizontalAlignment="Left" FontFamily="{StaticResource FontFamilyLogin}"/>
                        <TextBlock  Text="{Binding ContentTypeName, Mode=OneWay}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" Foreground="{StaticResource BrushGrey}" HorizontalAlignment="Left"/>
                    </StackPanel>
                </Grid>
            </StackPanel>
            <TextBlock Text="{Binding Time, Mode=TwoWay}" HorizontalAlignment="Right" Width="33" VerticalAlignment="Center" Margin="0" Foreground="{StaticResource BrushGrey}" FontFamily="{StaticResource FontFamilyLogin}" FontSize="{StaticResource FontSizeTextHome}" />
    
        </Grid>
    </UserControl>
    

    Pour le binding oui,

    Pour mieux expliquer mon problème:

    Il faut savoir que l'on se trouve dans un itemTemplate d'une list.

    <sharecontrols:UpPostControl AuthorCover="https://upload.wikimedia.org/wikipedia/commons/7/71/4416x3312-clouds-nature-sky-IMG_7627.jpg" />
    
    
    <sharecontrols:UpPostControl AuthorUsername="{Binding Author.Username}" />

    le premier marche mais pas le second.

    Cordialement,

    mardi 26 avril 2016 12:01
  • Bonjour,

    Aucune idée ?

    Cordialement

    jeudi 28 avril 2016 14:58