1
votes

I have a WPF window. Its DataContext is set to a ViewModel that contains a DataTable exposed as a property named dtPatientData. I have a datagrid (ComponentOne) that has its ItemsSource set to this property. The datagrid correctly binds the table and displays it. I want to also have a TextBox that binds to the value of a specific column(MRN) in the currently selected row. I'm failing. Here's my XAML:

                 <C1DataGrid:C1DataGrid
                    HorizontalAlignment="Left"
                    Margin="10,0,0,10"
                    VerticalAlignment="Bottom"
                    Width="504"
                    Height="143"
                    ItemsSource="{Binding dtPatientData}"
                    />
                <TextBox
                    Text="{Binding Source=dtPatientData, Path=MRN}"/>

I get the following:System.Windows.Data Error: 40 : BindingExpression path error: 'MRN' property not found on 'object' ''String' (HashCode=1384163063)'. BindingExpression:Path=MRN; DataItem='String' (HashCode=1384163063); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

I've tried all kinds of things. How can I do this in XAML?

1
But a column can have zero or more rows. - paparazzo
does your C1DataGrid has SelectedItem Property? - WPFUser

1 Answers

0
votes

You should data bind to the value of the MRN column of the DataGrid.SelectedItem. You can do that like this:

<C1DataGrid:C1DataGrid Name="DataGrid" HorizontalAlignment="Left" Margin="10,0,0,10"
    VerticalAlignment="Bottom" Width="504" Height="143" 
    ItemsSource="{Binding dtPatientData}" />

...

<TextBox Text="{Binding SelectedItem.MRN, ElementName=DataGrid}"/>