Question

J'ai deux contrôles Silverlight dans mon projet, tous deux ont des propriétés TeamId.Je voudrais les lier ensemble en XAML dans le contrôle hébergeant les deux contrôles utilisateur similaires à :

        <agChat:UserTeams x:Name="oUserTeams" />
        <agChat:OnlineUser x:Name="oOnlineUsers" TeamId="{Binding ElementName=oUserTeams, Path=TeamId}" />

Dans le premier contrôle, j'implémente System.ComponentModel.INOtifyPropertyChanged et je déclenche l'événement PropertyChanged lors du changement de propriété TeamId.

Dans le deuxième contrôle, j'ai utilisé l'extrait de code propdp pour identifier TeamId en tant que propriété de dépendance.

        // Using a DependencyProperty as the backing store for TeamId.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TeamIdProperty = 
        DependencyProperty.Register(
        "TeamId", 
        typeof(string), 
        typeof(OnlineUsers), 
        new System.Windows.PropertyMetadata(new System.Windows.PropertyChangedCallback(TeamChanged)));

Cependant, lorsque les contrôles Silverlight sont créés pour la première fois, j'obtiens l'exception suivante de Silverlight :

 Unhandled Error in Silverlight 2 Application Invalid attribute value {Binding ElementName=oUserTeams, Path=TeamId} for property TeamId. [Line: 21 Position: 146] at System.Windows.Application.LoadComponent(Object component, Uri xamlUri) at agChat.Page.InitializeComponent() at agChat.Page..ctor() at agChat.App.Application_Startup(Object sender, StartupEventArgs e) at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Avez-vous une idée de ce que je fais de mal ?Évidemment, tout cela pourrait être fait en code-behind, mais ceci semble comme la bonne approche.

Était-ce utile?

La solution

C'est la bonne approche dans WPF, mais pas dans Silverlight.

Vous ne pouvez pas vous lier à des éléments à l'aide de Xaml dans Silverlight.

Voici la ligne incriminée :TeamId="{Binding ElementName=oUserTeams, Path=TeamId}"

Plus précisément ElementName

Si vous le pouvez, placez l'objet de données dans Resources et déclarez-le là, alors vous pouvez faire ceci :

<agChat:UserTeams x:Name="oUserTeams" 
       DataContext="{StaticResource myDataObject}" />
<agChat:OnlineUser x:Name="oOnlineUsers" 
       DataContext="{StaticResource myDataObject}" 
       TeamId="{Binding  TeamId}" />
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top