WPF Combobox Selected Item Error - Showing “System.Data.Entity.DynamicProxies”

拈花ヽ惹草 提交于 2019-12-24 03:40:24

问题


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

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

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

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

Selected Item Busted, List Works

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

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!


回答1:


SOLVED - FOR OTHERS INFO

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

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

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>

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




回答2:


One difference between your two code samples is that in the second one, it sets ComboBox.DisplayMemberPath to something.

I believe that if this isn't set, the ComboBox will just call ToString() on the selected item. This would explain the value you're getting in your actual ComboBox.

DisplayMemberPath is just a string and expects a binding path, so you can't give it a multibinding unfortunately. I've never used the entity framework, but would it be possible to either override ToString() for the object you get back, or to add a property that contains the value you want and then use that for the value of DisplayMemberPath?




回答3:


If you are using entity framework code first check if your missing some virtual property in the model. Something like:

public class Equipment{
    ....
    public virtual Location Location {get; set;}
    ....
}


来源:https://stackoverflow.com/questions/18069889/wpf-combobox-selected-item-error-showing-system-data-entity-dynamicproxies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!