Sony
Sony

Reputation: 7196

Binding data grid template column combo box's selected item to data grid text column

In my project I've a data grid which contains template columns (combobox)and data grid text columns.How can i bind a data grid text column to a property in the selected item of the combobox in the template column.

for example, The first column is a template column that contains a combobox and the next column is a regular data grid text column. How can i bind the text column text to a property in the selected item of the combobox in the template column.

I've tried this but is giving binding error

<DataGrid 
Grid.Column="0" 
Grid.Row="3" 
Grid.ColumnSpan="9" 
AutoGenerateColumns="False" 
Margin="5"
CanUserDeleteRows="False" CanUserAddRows="False"
DataContext="{Binding}"
IsEnabled="{Binding EnableControls}" 
ItemsSource="{Binding SalesItemCollection}" 
EnableRowVirtualization="False"
EnableColumnVirtualization="False"
IsSynchronizedWithCurrentItem="False">

<DataGrid.Columns>

    <DataGridTemplateColumn Header="Batch" Width="*">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox x:Name="comboBox"
                          IsEditable="True"
                          MaxDropDownHeight="125"
                          DisplayMemberPath="BatchName"
                          VerticalAlignment="Stretch"
                          VerticalContentAlignment="Center"
                          IsSynchronizedWithCurrentItem="False"
                          SelectedValuePath="BatchId"
                          SelectedValue="{Binding BatchId}"
                          SelectedItem="{Binding Batch}"
                          ItemsSource="{Binding Path=DataContext.BatchColection, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>

    </DataGridTemplateColumn>

    <DataGridTextColumn IsReadOnly="True" Header="Expiry   " 
                        Binding="{Binding ElementName = comboBox, Path=SelectedItem.Expirydate}" />


</DataGrid.Columns>

I've tried changing Binding ElementName = comboBox to Binding Source={x:Reference comboBox}. But it is throwing exception. Can anyone tell me a solution for this.

Upvotes: 0

Views: 462

Answers (1)

Krishna
Krishna

Reputation: 1985

As you are already binding selected Item to Batch use that, Try this

<DataGridTextColumn IsReadOnly="True" Header="Expiry   " 
                    Binding="{Binding Source=Batch, Path=Title}" />

Upvotes: 1

Related Questions