WPF组合框所选项目错误-显示"System.Data.Entity.DynamicProxies"; [英] WPF Combobox Selected Item Error - Showing "System.Data.Entity.DynamicProxies"

查看:51
本文介绍了WPF组合框所选项目错误-显示"System.Data.Entity.DynamicProxies";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了无数次尝试和论坛帖子,但是我仍然无法解决我的问题.

i've been through tons of attempts and forum posts but i still can't solve my issue.

问题 显示来自实体框架dbcontext的数据的组合框不会显示所选的值,但可以用于项目列表. 所选项目仅显示

ISSUE A combobox displaying data from an entity framework dbcontext does not display the selected value but DOES work for the item list. The selected item just shows

System.Data.Entity.DynamicProxies.Equipment_37EBC79AEAECCCCD132FD15F1C9172DF4DD402B322A9C5762AE640F03887F702

System.Data.Entity.DynamicProxies.Equipment_37EBC79AEAECCCCD132FD15F1C9172DF4DD402B322A9C5762AE640F03887F702

但组合框列表正确显示....

BUT the list of the combobox displays correctly....

设置 我有一个dbcontext,其中包含一个名为equipment的类. 设备有两个要显示的项目 字符串标签; Location.Name;

SETUP I have a dbcontext that contains a class named equipment. Equipment has two items i want to display String Tag; Location.Name;

选中的项目已删除,列表有效

  <ComboBox x:Name="cbxCopyTo" Grid.Row="2" Grid.Column="1"
                          IsEditable="True" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False"
                          ItemsSource="{Binding}">
                    <ComboBox.SelectedValue>
                        <DataTemplate>
                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" >
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0} ({1})">
                                        <Binding Path="Tag" />
                                        <Binding Path="Location.Name" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </DataTemplate>
                    </ComboBox.SelectedValue>
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" >
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0} ({1})">
                                        <Binding Path="Tag" />
                                        <Binding Path="Location.Name" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

您可以在上方看到我什至尝试显式设置所选值;但这没用. 我确实注意到,当我尝试使用转换器时,将转换器放入其中时从未调用过SelectedItem或SelectedValue.

You can see above i even tried explicitly setting the selected value; but it didn't work. I did notice when i tried using a converter that it was never called for SelectedItem or SelectedValue when i put converters in there.

如果我忽略位置(从数据源拖放获得),则下面的工作. 这样可以正确显示列表和所选项目.

The below works if i ignore location (got from datasource drag and drop). This show both the list and selected item correctly.

<Label Grid.Row="1" Grid.Column="0" Content="Copy From:" />
                <ComboBox x:Name="cbxCopyTo" Grid.Row="1" Grid.Column="1"
                          IsEditable="True" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False"
                          DisplayMemberPath="Tag" ItemsSource="{Binding}">
                    <ComboBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </ComboBox.ItemsPanel>
                </ComboBox>

请帮助;我将不胜感激!

Please help; i would be greatly appreciated!

推荐答案

已解决-供其他人使用的信息

好吧,我想出了一种创建串联属性(如@Andy建议)的方法,但该属性没有出现在数据库中.

Ok i figured out way to make a concatenated property (like @Andy suggested) BUT without it appearing in the database.

通过使用代码优先注释,您可以在EF模型上声明一个属性,该属性不会映射到数据库,但可以像任何db属性一样被查询或绑定.这是在您的EF模型类的声明中完成的,如下所示:

By using code first annotations you can declare a property on the EF model that doesn't get mapped to the database but can be queried or binded like any db property. This is done in the declaration of your EF model class, like below:

/// <summary>
        /// Creates concatenation object that will not be mapped in the database but will be in the
        /// Object Relational Mapping (ORM) of the EF model.
        /// </summary>
        [NotMapped]
        public string TagAndLocation { get { return Tag + " (" + Location.Name + ")"; } } 

然后,这使我可以通过以下XAML使用对"TagAndLocation"的简单绑定:

This then allows me to use the simple binding to "TagAndLocation" with the below XAML:

        <ComboBox x:Name="cbxCopyTo" Grid.Row="2" Grid.Column="1"
                  IsEditable="True" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False"
                  DisplayMemberPath="TagAndLocation" ItemsSource="{Binding}">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>

再次感谢@Andy和@aguedo valdes花时间提出建议.

Thanks again to @Andy and @aguedo valdes for taking the time to make suggestions.

这篇关于WPF组合框所选项目错误-显示"System.Data.Entity.DynamicProxies";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆