简体   繁体   English

具有DisplayValue和SelectValue的WPF组合框无法正常工作

[英]WPF Combobox with DisplayValue and SelectValue not working correctly

I guess I'm missing something here and I hope someone can help me out. 我想我在这里想不到的东西,希望有人能帮助我。 I have this class I'm using in Entity Framework: 我在实体框架中使用了此类:

    public VendorDetail()
    {
        public long VendorID { get; set; }
        public string VendorName { get; set; }
        public string Address { get; set; }
    }

In SQL Server 2008, I have this table called PartsList that has a foreign key relationship with the VendorDetail table. 在SQL Server 2008中,我有一个名为PartsList的表,该表与VendorDetail表具有外键关系。

In the XAML, I have a Datagrid: 在XAML中,我有一个Datagrid:

   <Grid DataContext="{StaticResource PartsListsSource}">
    <DataGrid x:Name="partsListsDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
            ItemsSource="{Binding}"
            RowDetailsVisibilityMode="VisibleWhenSelected">
               <DataGrid.Columns>
                   <DataGridTextColumn x:Name="partsListIDColumn" Binding="{Binding PartsListID}"
                    Header="Parts List ID" Width="SizeToHeader" />
                   <DataGridTextColumn x:Name="partIDColumn" Binding="{Binding PartID}" Header="Part ID"
                    Width="SizeToHeader" />
                        <DataGridTemplateColumn x:Name="vendorNameColumn"
                                          Width="Auto"
                                          Header="Vendor Name">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.VendorDetails}"
                                              DisplayMemberPath="VendorName"
                                              SelectedValuePath="VendorID"
                                              SelectedItem="{Binding VendorID}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn x:Name="vendorPartNoColumn" Binding="{Binding VendorPartNo}"
                        Header="Vendor Part No" Width="SizeToHeader" />
                    <DataGridTextColumn x:Name="descriptionColumn" Binding="{Binding Description}"
                        Header="Description" Width="SizeToHeader" />
                </DataGrid.Columns>
    </DataGrid>
</Grid>

Now all the other columns are working except the ComboBox. 现在,除了ComboBox之外,所有其他列都在工作。

I know the VendorID is a bit confusing, so just to clarify: I've created a list of VendorDetail that I want the ComboBox to use, use the VendorDetail.VendorID and VendorDetail.VendorName, and it set to the PartLists.VendorID which is the Foreign Key to VendorDetail. 我知道VendorID有点令人困惑,因此只需澄清一下:我创建了想要ComboBox使用的VendorDetail列表,使用VendorDetail.VendorID和VendorDetail.VendorName,并将其设置为PartLists.VendorID,这是VendorDetail的外键。

So what is it that I'm doing wrong? 那我做错了什么呢?

Someone in another forum told me the problem. 在另一个论坛上有人告诉我这个问题。 I should use SelectedValue instead SelectedItem. 我应该使用SelectedValue而不是SelectedItem。 So another words, it should be this: 换句话说,应该是这样的:

SelectedValue="{Binding VendorID}"

And that took care of the problem. 这就解决了这个问题。 I thought it was something so simple! 我以为这很简单!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM