簡體   English   中英

WPF組合框所選項目錯誤-顯示“ System.Data.Entity.DynamicProxies”

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

我已經經歷了無數的嘗試和論壇帖子,但是我仍然無法解決我的問題。

問題顯示來自實體框架dbcontext的數據的組合框不會顯示所選的值,但可以用於項目列表。 所選項目僅顯示

System.Data.Entity.DynamicProxies.Equipment_37EBC79AEAECCCCD132FD15F1C9172DF4DD402B322A9C5762AE640F03887F702

但是組合框的列表顯示正確。

安裝我有一個dbcontext,其中包含一個名為equipment的類。 設備有兩個要顯示String Tag的項目; 地點名稱;

選中的項目已刪除,列出了工程

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

如果我忽略位置(從數據源拖放獲得),則下面的工作。 這樣可以正確顯示列表和所選項目。

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

請幫忙; 我將不勝感激!

已解決-其他信息

好的,我想出了一種制作串聯屬性(如@Andy建議)但沒有出現在數據庫中的方法。

通過使用代碼優先注釋,您可以在EF模型上聲明一個屬性,該屬性不會映射到數據庫,但可以像任何db屬性一樣被查詢或綁定。 這是在EF模型類的聲明中完成的,如下所示:

/// <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”的簡單綁定:

        <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抽出寶貴的時間提出建議。

您的兩個代碼示例之間的區別是在第二個示例中,它將ComboBox.DisplayMemberPath設置為某種值。

我相信,如果未設置此項,則ComboBox只會在所選項目上調用ToString() 這將解釋您在實際ComboBox獲得的值。

DisplayMemberPath只是一個字符串,需要綁定路徑,因此很遺憾,不能給它一個多重綁定。 我從未使用過實體框架,但是是否可以為返回的對象重寫ToString()或添加包含所需值的屬性,然后將其用作DisplayMemberPath的值?

如果使用的是實體框架代碼,請首先檢查模型中是否缺少某些虛擬屬性。 就像是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM