数据绑定到 DomainDataSource 查询参数

发布于 2024-08-10 12:45:08 字数 2065 浏览 0 评论 0原文

我需要将用户名绑定到 DomainDataSource QueryParameter。我的理解是以下内容不起作用:

<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>

我不反对使用页面的 C# 代码隐藏部分,但我不确定将其放入哪个事件中。

到目前为止我已经尝试过此操作:

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

但是自从 InitializeComponent () 首先触发,并加载数据,这会导致 DomainDataSource 由于查询没有任何要运行的参数而崩溃......它不起作用。

接下来我尝试了这个...

[xaml 文件]

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

[cs 文件]

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

不幸的是,该事件从未触发。我不知道为什么。

我什至尝试过这个: [xaml 文件]

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

[cs 文件]

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

但这只是愚蠢的。

我不知所措。如何在页面加载时使用参数加载此查询?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-08-17 12:45:08

嗯,我不是你问题的具体答案,但我可能知道一种完全避免这种情况的方法。

我注意到您有一个名为“GetStockByCompany”的方法,该方法接受当前登录的用户作为参数...

您可以完全删除对该参数的需要,而是在服务器端查询“GetStockByCompany”时在“Where”中使用它部分:

this.ServiceContext.User.Identity.Name

Ex - 获取当前登录用户的所有相册:

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

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();
唯憾梦倾城 2024-08-17 12:45:08

绑定查询参数是可行的,典型用法是将其直接绑定到控件。

要在代码隐藏中设置参数,请为参数指定名称并设置 value 属性。无需在后面的代码中添加整个参数。

Binding the query parameter works, the typical usage is that you bind it directly to controls.

To set the parameter in code behind, give the parameter a name and set the value property. There's no need to add the whole parameter in code behind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文