How do I display a different list than the source item when opening the combo box in WPF?

Mojtaba_Hakim 281 Reputation points
2022-08-29T10:41:47.407+00:00

I'm using C# WPF with SQL Server database

I have DataGrid that filled by Factor table from Database (ItemSource)

and inside of this DataGrid, I have a ComboBox that is also filled with different table as Commodity that related to Factor

What I need : I want the Combobox I have to be filled with a list from my Commodity table, but what is selected as the value is from the data grid (from the value filled in each row).

full information : how-do-i-display-a-different-list-than-the-source-item-when-opening-the-combo-bo

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} vote

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-08-30T02:21:54.553+00:00

    @Mojtaba_Hakim , based on my test, the answer from the stack overflow is correct.

    You could make the following changes:

     <ComboBox x:Name="ComboBox_Commodity"  
                                              ItemsSource="{Binding Path=TheCommodityCombo_DATA, RelativeSource={RelativeSource AncestorType=Window}}"  
                                              DisplayMemberPath="CommodityName"  
                                              SelectedValuePath="CommodityCode"  
                                              IsTextSearchEnabled="True"  
                                              IsEditable="True"  
                                              SelectedIndex="0"  BorderBrush="#FFADEEB4" Background="{x:Null}" BorderThickness="1" SelectedValue="{Binding CommodityCode}">  
      
                                    <!--For ComboBox_Commodity ↑-->  
                                    <!--SelectedValue="{Binding The selected value of each combo box is proportional and based on the value of that row in the datagrid}"-->  
                                    <ComboBox.ItemsPanel>  
                                        <ItemsPanelTemplate>  
                                            <VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"/>  
                                        </ItemsPanelTemplate>  
                                    </ComboBox.ItemsPanel>  
                                </ComboBox>  
    

    Result:

    235951-image.png

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful