wpfbindingdatagridviewcomboboxcell

Binding Textblock text inside a combobox grid cell


I'm using a combobox cell inside a datagridview through datatemplate.

I bind an item source and set the DisplayMemberPath , SelectedValuePath and SelectedValue proprieties on the combobox inside

Once a item in the combobox is selected I would like to show the DisplayMemberPath text on the textblock element, I just don't know how to bind it.

<DataGridTemplateColumn>

    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox   
ItemsSource="{Binding Path=DataContext.PartNumbers, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                        DisplayMemberPath="PartNumberDescription"        
                        SelectedValuePath="PartNumberCodeCode"
                        SelectedValue="{Binding Code}"/>
          </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>

    <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                    <TextBlock Text="{How can I bind DisplayMemberPath here?}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

If I use the same bind of SelectedValue, it works and the value is displayed, but I would like to show the description.

<TextBlock Text="{Binding Code}"/>
<!-- It works, but I would like to show the text of the combobox, not the value -->

Solution

  • XAML

    <TextBlock Text="{Binding Description}"/> 
    

    ViewModel

    public string Description {
        get {
            return PartNumbers.SingleOrDefault(x => x.PartNumberCodeCode == Code)?.PartNumberDescription;
        }
    }
    

    On property change of Code, notify property change of Description