Using DataGrid as DataContext

Dec 13, 2008 at 12:42 PM
Hi, I was hoping someone could help me with this problem; I want to bind a field of the currently selected row of my DataGrid to a TextBlcok so that as the SelectedRow (or maybe CurrentRow) of the DataGrid changes the text in the TextBlock changes as well. I thought I could do this by setting the DataContext of the Grid that the TextBlock is in to the DataGrid as shown below:

    <Grid Grid.Row="0" Grid.Column="2" ShowGridLines="False" Background="White" Name="memberDetailsGrid"
       DataContext="{Binding ElementName=nADsDataGrid, Path=SelectedItem, Mode=OneWay}">
     <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
     <Border BorderBrush="Black" BorderThickness="1" Grid.ColumnSpan="10" Grid.RowSpan="10">
      <StackPanel Grid.ColumnSpan="10">
       <TextBlock Height="20" Background="LightSteelBlue" FontSize="10pt">Member details</TextBlock>
       <TextBlock Name="xyz" Grid.ColumnSpan="5" Grid.RowSpan="2" Text="{Binding Path=Surname, Mode=OneWay}"/>
       <Button Margin="1" Name="button1" Click="button1_Click" Grid.ColumnSpan="6" Grid.Row="1">
          Button</Button>
       <TextBox Name="Output" Grid.ColumnSpan="6"/>
      </StackPanel>
     </Border>
    </Grid>

but I realised that because I have defined my own UserControl to contain my DataGrid:

    <myDataGrid:DataGridWithAutoFilter Name="nADsDataGrid"
           FontStyle="Normal" FontWeight="Normal"
            Grid.ColumnSpan="3" Grid.Row="2"
            MaxHeight="1200"  MaxWidth="1920"/>

this doesn't work. Does anybody know how to make this work?

Thanks

Dec 15, 2008 at 10:12 AM
I don't know if that is the correct approach, but one thing I would look into is the IsSynchronizedWithCurrentItem property on the DataGrid.
Dec 15, 2008 at 9:56 PM
Thanks mikj.
 
Yes I have looked into this but the real problem I am now facing is that the way I have created "myDataGrid", it appears as a UserControl and not a DataGrid so these properties are not visible in the context I am using it. I think need to derive my custom DataGrid in a different way which I am now investigating.

I think I need some help with that too - I will start a new thread.
Coordinator
Dec 16, 2008 at 2:19 AM
You can have your UserControl have a reference to your DataGrid, through a property maybe.  So your DataContext binding could bind to the ElementName=nADsDataGrid.DataGrid. That isn't very MVVM but that is an option.  As another option, you can have a ViewModel that has a CurrentItem property that gets notifications on the DataGrid.CurrentItem changes.