简体   繁体   中英

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 -->

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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