Pregunta

Tengo dos controles Silverlight en mi proyecto, ambos tienen propiedades TeamId. Me gustaría unirlos en XAML en el control que aloja ambos controles de usuario similares a:

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

En el primer control, estoy implementando System.ComponentModel.INotifyPropertyChanged y subo el evento PropertyChanged cuando cambia la propiedad TeamId.

En el segundo control, he usado el fragmento de propdp para identificar el TeamId como una propiedad de dependencia.

        // 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)));

Sin embargo, cuando se crean los controles de Silverlight por primera vez, recibo la siguiente excepción 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)

¿Alguna idea de lo que estoy haciendo mal? Obviamente, todo esto podría hacerse en código subyacente, pero esto parece como el enfoque correcto.

¿Fue útil?

Solución

Ese es el enfoque correcto en WPF, pero no en Silverlight.

No puede enlazar elementos usando xaml en Silverlight.

Esta es la línea ofensiva:  TeamId = " {Binding ElementName = oUserTeams, Path = TeamId} "

Específicamente ElementName

Si puede, coloque el objeto de datos en Recursos y declare allí, entonces puede hacer esto:

<agChat:UserTeams x:Name="oUserTeams" 
       DataContext="{StaticResource myDataObject}" />
<agChat:OnlineUser x:Name="oOnlineUsers" 
       DataContext="{StaticResource myDataObject}" 
       TeamId="{Binding  TeamId}" />
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top