silverlight-3.0domaindatasource

Databinding to a DomainDataSource Query Parameter


I need to bind a username to a DomainDataSource QueryParameter. My understanding is that the following does not work:

<RiaControls:DomainDataSource x:Name="MyData" LoadSize="20" QueryName="GetStockByCompany" AutoLoad="True">

    <RiaControls:DomainDataSource.DomainContext>
        <ds:InventoryDomainContext  />
    </RiaControls:DomainDataSource.DomainContext>

    <RiaControls:DomainDataSource.QueryParameters>
        <riadata:Parameter 
            ParameterName="userName" 
            Value="{Binding Path=User.Name}" />
    </RiaControls:DomainDataSource.QueryParameters>

</RiaControls:DomainDataSource>

I am not opposed to using the C# code-behind part of the page, but I'm not sure what event to put this in.

So far I've tried this:

public Inventory()
{
    InitializeComponent();
    Loaded += Inventory_Loaded;
}
private void Inventory_Loaded(object sender, RoutedEventArgs e)
{
   this.MyData.QueryParameters.Add(new Parameter { ParameterName = "userID", Value = RiaContext.Current.User.Name});
}

But since InitializeComponent() fires first, and loades the data, which causes the DomainDataSource to bomb due to the Query not having any parameters to run... it didn't work.

Next I tried this...

[xaml file]

<RiaControls:DomainDataSource x:Name="MyData" LoadSize="20" QueryName="GetStockByCompany" AutoLoad="True" LoadingData="MyData_LoadingData">

[cs file]

private void MyData_LoadingData(object sender, LoadingDataEventArgs e)
{
   this.MyData.QueryParameters.Add(new Parameter { ParameterName = "userID", Value = RiaContext.Current.User.Name});
}

Unfortunately, the event never fired. I'm not sure why.

I even tried this: [xaml file]

<RiaControls:DomainDataSource x:Name="MyData" LoadSize="20" QueryName="GetStockByCompany" AutoLoad="True" LoadedData="MyData_LoadedData">

[cs file]

private void MyData_LoadedData(object sender, LoadedDataEventArgs e)
{
   this.MyData.QueryParameters.Add(new Parameter { ParameterName = "userID", Value = RiaContext.Current.User.Name});
}

But that was just dumb.

I'm at a loss. How do I load this query, with the parameter, as the page loads?

Thanks!


Solution

  • Hmmm I not a specific answer to your problem but I may know a way to avoid the situation entirely.

    I noticed you have a method named "GetStockByCompany" that accept the currently logged in user as a parameter...

    You can completely remove the need for the parameter and instead on your server side query for "GetStockByCompany" use this in your "Where" part:

    this.ServiceContext.User.Identity.Name

    Ex - Getting all the albums for the currently logged in user:

    album = this.Context.AlbumSet
                .Where(n => n.AlbumId == AlbumId)
                .Where(n => n.aspnet_Users.UserName == this.ServiceContext.User.Identity.Name)
                .First();