Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 420
Asvaldr Avatar asked Mar 11 '26 06:03

Asvaldr


1 Answers

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.

like image 83
Asvaldr Avatar answered Mar 13 '26 20:03

Asvaldr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!