WPF DataGridComboBoxColumn Binding

Hi,

I have a WPF application that contians a datagrid. I have a DataGridComboBoxColumn that I want to bind to a list but cannot.

I want the combobox to only display two values.

public List<string> StatusList;
StatusList = new List<string>();
            StatusList.Add("Hold");
            StatusList.Add("Send");

Open in new window


When the datagrid is populated it is bound to my object OrderBlock of type OrderBlocks, below.

class OrderBlocks
{
   public string setting;
   public List<Order> Orders;
}

class Order
{
  public double Amount;
  public int Name;
  public string StatusGood;
}

Open in new window


XAML Code
<DataGrid DataContext="{Binding OrderBlock}"
                      Name="dataGridOrders" 
                      ItemsSource="{Binding Orders}"


 <DataGridComboBoxColumn Header="Status Good" SelectedItemBinding="{Binding StatusList}" SelectedValuePath="{Binding StatusGood}"/>

Open in new window


Nothing works no matter what I do the combobox is just empty.

Any help would be great,
thanks,
M
mcs26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gautham JanardhanCommented:
try this

<DataGridComboBoxColumn>
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
                            <Setter Property="IsReadOnly" Value="True"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>

Open in new window


you may want to set other properties for the combo like display member path and value member path
0
mcs26Author Commented:
Hi gauthampj,

Thanks for the reply. It still doesn't show anything in the combobox sadly. Here is all the code to do with my data grid.

 <!-- Grid that contains the DataGrid which shows the list of orders -->
        <Grid Grid.Row="2" x:Name="GridOrders">                    
            <!-- The data grid to display errors-->
            <DataGrid DataContext="{Binding OrderBlock}"
                      Name="dataGridOrders" 
                      ItemsSource="{Binding Orders}"
                      Style="{StaticResource DataGridTemplate}"
                      ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"                      
                      RowHeaderStyle="{StaticResource DG_RowHeader}"
                      RowStyle="{StaticResource DG_Row}"
                      CellStyle="{StaticResource DG_Cell}"
                      AutoGenerateColumns="False"
                      HorizontalAlignment="Stretch" 
                      VerticalAlignment="Stretch"
                      Background="Silver"
                      Margin="25,5,20,15">                                                     

                <DataGrid.Columns>
                    <DataGridComboBoxColumn Header="Action">
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
                                <Setter Property="IsReadOnly" Value="True"/>
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>
                    <!--<DataGridComboBoxColumn Header="Action" SelectedItemBinding="{Binding StatusGood}" ItemsSource="{Binding Path=StatusList, Source={StaticResource ComboItems}}" />-->
                    <!--<DataGridTextColumn Header="Action" Binding="{Binding StatusGood}" IsReadOnly="False"/>-->
                    <DataGridTextColumn Header="Fund" Binding="{Binding Account}" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Security ID" Binding="{Binding SecurityID}" IsReadOnly="False"/>
                    <DataGridTextColumn Header="Security Name" Binding="{Binding SecurityName}" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Buy/Sell" Binding="{Binding TransType}" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Quantity" Binding="{Binding OrderQunatity}" IsReadOnly="False"/>
                    <DataGridTextColumn Header="Currency" Binding="{Binding BuyCurrency}" IsReadOnly="False"/>
                    <DataGridTextColumn Header="Manager" Binding="{Binding FundManager}" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Order Reason" Binding="{Binding OrderReason}" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Reject Reason" Binding="{Binding RejectReason}" IsReadOnly="True" Width="*"/>                    
                </DataGrid.Columns>
            </DataGrid>

Open in new window

0
Gautham JanardhanCommented:
you are setting datacontext as OrderBlocks and that class need to have the StatusList collection, is that the case?
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

mcs26Author Commented:
I have moved StatusList into the class OrderBlocks. The combobox is now showing "Send" & "Hold" in the drop down.

The only issue is that its not showing the current value of the items from my List<Orders>. So when the data grid loads up the column with my combobox shows no values for any item. However I can see the two options in the drop down.

Cheers!
0
Gautham JanardhanCommented:
try this as style, you need to set the selected value binding also

<Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Source}"/>
                            <Setter Property="IsReadOnly" Value="True"/>
                            <Setter Property="SelectedValue" Value="{Binding StatusGood}"/>
                        </Style>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcs26Author Commented:
Sorry does this go under the <DataGridComboBoxColumn.ElementStyle> section or the <DataGridComboBoxColumn.EditingElementStyle> section?
0
Gautham JanardhanCommented:
both
0
mcs26Author Commented:
brillant thank-you for your patience
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.