Silverlight / 数据绑定 / EntityRef

发布于 2024-11-15 00:54:40 字数 1898 浏览 2 评论 0原文

我一直有一个数据绑定程序,我认为这是一个相当常见的情况,但我似乎找不到任何人有类似的问题 - 这让我认为我错过了一些东西。

简而言之,在保存一个新的之后父实体,父实体似乎无法在绑定操作期间引用 EntityRef 并引用相关实体上的属性……。细节来了……

我有一个如下所示的层次结构:组织有一个或多个代表,其中每个代表都是一个人,每个人都有零个或多个电话号码。

所以组织->>代表->人员->>电话号码。

我使用列表框在左侧显示代表列表,右侧显示所选代表的详细信息。我使用列表框中选定的项目作为显示人员信息的网格的源。

我正在使用 WCF RIA 服务,因此我有一个名为 Organization 的实体对象,其中包含代表的 EntitySet 集合,我将其用作绑定到列表框的项目源。

每个代表中都有一个 Person 对象,它引导我找到 Person 的名字和 姓氏(在内部,这是 EntityRef 发挥作用的地方)。

在代表框中,我想显示代表头衔及其相关的名字和姓氏(位于相关实体人员上)。因此列表框将包含如下内容:

标题 名字 姓氏 区域经理比尔·史密斯 区域经理 John Doe

XAML 如下所示:

 <ListBox> 
   <ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>

           <Grid.ColumnDefinitions>

             <ColumnDefinition/>

             <ColumnDefinition/>

             <ColumnDefinition/>

           </Grid.ColumnDefinitions>

           <TextBlock Grid.Column="0" Text="{Binding ContactTitle}" Margin="5"/>

          <TextBlock Grid.Column="1" Text="{Binding Path=Person.FirstName, Mode=OneWay,    FallbackValue='New Representative'}" Margin="5"/>

          <TextBlock Grid.Column="2" Text="{Binding Path=Person.LastName, Mode=OneWay, FallbackValue='New Representative'}" Margin="5"/>

        </Grid>

     </DataTemplate>

  </ListBox.ItemTemplate>

</ListBox>

当我创建新代表时,我还创建一个新人员记录,并立即将新人员与代表关联,并将代表与组织关联。

在我保存 DomainContext 之前,一切都运行良好。当我通过 RIA 服务提交更改时,我已经通过提交跟踪了所有更改,并且它更新数据库工作完美。提交操作完成后返回的提交结果看起来也不错。

我已确认组织、代表和人员在客户端应用程序中均正确关联。

然而,当我返回列表框时,该人的名字和姓氏都丢失了!我花了几个小时(实际上是几天)试图确定原因,但似乎无法找到一种方法来跟踪 Submitcallback 和返回屏幕之间发生的情况。

在调试中,我添加了一个 SelectionChanged 事件处理程序,并添加了一个断点。当我单击新保存的行并查看 Person 时(它不为空,并且包含正确的数据),但列表框正在显示 FallBack 属性。

为什么数据绑定在提交操作完成后收到空值,但当我在选择更改时遇到断点时它仍然存在。我确信这是一个时间问题 - 但肯定这个要求以前已经得到满足!

I been having a DataBinding program that I would think be a fairly common occurrance, but I can't seem to find anybody with a similar problem - which leads me to think that I'm missing something..

In short, after saving a new Parent entity, the Parent Entity cannot seem to refer to an EntityRef during binding operations and refer to attributes on a related Entity…. here comes the detail…

I have a hierarchical structure that looks like this: Organization has one or more Representatives, where each Representative is a Person, each Person that has zero or more Telephone Numbers.

So Organization->>Representative->Person->>TelephoneNumbers.

I’m using a list box to show a list of Representatives on the left, and the right side displays the detail information for the Representative that is selected. I used the selected item in the ListBox as the source for a grid that displays Person information.

I’m using WCF RIA Services, so I have an Entity object called Organization, which contains an EntitySet collection of Representatives, which I use as the itemsource to bind to the list box.

Within each Representative there is a Person object, which leads me to the Person First name and
Last Name (internally this is where the EntityRef comes to play).

In the Representative box, I would like to show the Representative Title, as well as their related First and Last Name (which is on the related entity Person). So the list box would contain something like this:

Title First Name Last Name
District Manager Bill Smith
Area Manager John Doe

XAML looks like this:

 <ListBox> 
   <ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>

           <Grid.ColumnDefinitions>

             <ColumnDefinition/>

             <ColumnDefinition/>

             <ColumnDefinition/>

           </Grid.ColumnDefinitions>

           <TextBlock Grid.Column="0" Text="{Binding ContactTitle}" Margin="5"/>

          <TextBlock Grid.Column="1" Text="{Binding Path=Person.FirstName, Mode=OneWay,    FallbackValue='New Representative'}" Margin="5"/>

          <TextBlock Grid.Column="2" Text="{Binding Path=Person.LastName, Mode=OneWay, FallbackValue='New Representative'}" Margin="5"/>

        </Grid>

     </DataTemplate>

  </ListBox.ItemTemplate>

</ListBox>

When I create a new Representative, I also create a new Person record, and immediate associate the new Person with the Representative, and the Representative with an Organization.

Everything works great until I save the DomainContext. When I submit the changes back via RIA Services, I’ve traced it all through the Submit, and it updates the DB works perfectly. The Submitted Results, which are returned after the submit operation is completed, looks good as well.

I’ve confirmed that the Org, Rep, and Person are all related correctly in the client side application.

YET, when I return to the Listbox, both the first and last name of the Person ARE LOST! I’ve spent hours and hours (days in fact) trying to determine why, but can’t seem to find a way to trace what happens between the Submitcallback, and the return to the screen.

In my debugging, I added a SelectionChanged event handler, and added a break point. When I click on the new saved row, and look at the Person – it’s not null, and it contains the correct data - yet the list box is displaying the FallBack attribute.

Why does Databinding receive a null after the Submit operation completes, yet it is there when I hit my breakpoint on selectionchanged. I’m sure it’s a timing issue – but surely this requirement has been met before!

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

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

发布评论

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

评论(3

愚人国度 2024-11-22 00:54:40

IMO,你正在丢失你的绑定,我不确定,但是当你在绑定时手动设置其数据时,就会发生这种情况。

IMO you are losing your bindig, I'm not sure but this situation happens when you set its data manually while it has been binded.

岁月蹉跎了容颜 2024-11-22 00:54:40

这有点棘手。为了调试绑定,我使用一个假转换器,并在如下所示的注释行处放置一个断点:

public class DummyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value; // <- place you break point here
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

并且您需要在 xaml 中使用它:

<Grid>
    <Grid.Resources>
        <local:DummyConverter x:Key="DummyConv"/>
    </Grid.Resources>

    <TextBlock Text="{Binding MyText,Converter={StaticResource DummyConv}}"/>
</Grid>

现在您可以看到转换器中的值如何变化以及它是否未达到打破品脱这意味着你已经失去了你的绑定(或者你在你的DataContext中没有这样的属性)

This is a little bit tricky. To debug the binding i use a fake convertor and placing a break point at the commented line shown bellow:

public class DummyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value; // <- place you break point here
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

and you need to use it in your xaml :

<Grid>
    <Grid.Resources>
        <local:DummyConverter x:Key="DummyConv"/>
    </Grid.Resources>

    <TextBlock Text="{Binding MyText,Converter={StaticResource DummyConv}}"/>
</Grid>

and now you can see how the value in the Converter changes and if it does not reaches the break pint it means you have lost your binding (or you have no such property in your DataContext)

陌路黄昏 2024-11-22 00:54:40

我想我明白为什么这是一个问题,但我仍然不明白为什么这对很多人来说不是一个问题。

我使用代理键,这个问题仅在提交后发生。我已经深入了解了客户端 DomainService 模型在添加后是否正确保存了这些数据,现在我确信它是正确的。

不过,我认为这是一个时间问题。必须先将父级添加到模型中,然后再添加子级。然而,网格绑定到父级,并参考导航属性来显示子级数据。在这种情况下,基数是一对一的,但是第一个基数的负载必须在第二个基数的负载发生之前发生。

我的想法是加载一个实体,因此属性会增加。该控件响应 raise 属性并获取数据。然而,这种情况发生在加载“子”实体之前,并且 EntityRef 导航属性返回 null。然后直接加载子实体,但由于绑定已经发生在父实体上,因此控件不会“看到”数据。

我发现如果我重新绑定控件(Silverlight),一切都会显示出来。然而,在 MVVM 中,VM 完成加载与必须重新绑定的控件之间存在紧密耦合。我尝试重新引发属性更改事件,但控件没有响应。

我将不得不花时间创建一个带有错误的小项目,并将其传递给技术支持以进行澄清。

I think I understand why this is a problem, but I still don't understand why it's not more of a problem to a lot of people.

I use surrogate keys, and this problem only occurs after a submit. I have gone pretty deep to see if the client side DomainService model is holding this data correctly after an add, and I'm now convinced it is.

However, I think this is a timing issue. The parent has to be added first into the model, before the children. Yet the grid is bound to the parent, with reference to navigation properties to display children data. In this case the cardinalty is one-to-one, but the load of one has to happen until the load of the second one.

My thought here is that one entity is loaded, and hence properties raises. The control responds to the raise property, and gets the data. This happens however before the "child" entity is loaded, and the EntityRef navigation property returns a null. The child entities are then loaded directly after, but because the binding has already occurred on the parent, the control does not "see" the data.

I found that if I rebind the control, (Silverlight), everything shows up. However, in MVVM, this is a tight coupling between the VM finishing a load, and the control having to do a rebind. I've tried re-raising the property changed events, but the control does not respond.

I will have to take the time to create a small project with the error, and pass it on to tech support for clarification.

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