0

My Code is as follows

MainWindow.Xaml

      <ListBox Width="400" Margin="10" x:Name="myListBox" 
         ItemsSource="{Binding Path=GridVal}" SelectedItem="{Binding CurrentItem}">
              <ListBox.ItemTemplate>
                   <DataTemplate>
                     <Expander Header="Header1" IsExpanded="True">
                        <StackPanel>
                          <DataGrid
                             x:Name="dataGrid"
                             AutoGenerateColumns="False"
                             ItemsSource="{Binding Path=GridVal}" 
                             Height="250" Width="250" SelectedItem="{Binding CurrentItem}"/>
                    </StackPanel>
                </Expander>
        </DataTemplate>
        </ListBox.ItemTemplate>

MainWindow.Xaml

  public object CurrentItem
  {
    get{return _item;}
    set{_item=value;}
  }

It displays a Listbox which has Expander and a Datagrid

I want to get the current selected row in datagrid which I am not able to get at this point.I am getting Datatemplate items instead of Datagrid

Rohit
  • 10,056
  • 7
  • 50
  • 82
  • use raiseonproperty change or onpropertychange RaiseonPropertyChange("CurrentItem"); @kyle – Dhru 'soni Mar 03 '15 at 13:46
  • 1
    it seems you're binding to `CurrentItem` for both the list and the `DataGrid`. Are you sure that you are inspecting the correct `CurrentItem`? It should be the one in the class that also has the property `GridVal` – default Mar 03 '15 at 13:54
  • 1
    Do you see any binding errors in the debug output? Could you show your viewmodels that you are using as datacontexts? – default Mar 03 '15 at 14:01
  • Yes It is `SelectedItem` not `SelectedIndex`..But still the problem persists. – Rohit Mar 03 '15 at 14:07

2 Answers2

1

You have used the wrong property to data bind to your CurrentItem property. Instead of SelectedIndex, you should data bind to the DataGrid.SelectedItem property:

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" 
    ItemsSource="{Binding GridVal}" Height="250" Width="250" 
    SelectedItem="{Binding CurrentItem}" />

Although, as @Default mentioned, you are also trying to data bind the CurrentItem property to two different control properties, so you'll need to add a different property to get this to work properly. Furthermore, the CurrentItem property should be of the same type as the items in the GridVal collection.


UPDATE >>>

You seem to be missing some information as you are also trying to data bind the same collection to the ListBox.ItemsSource and the DataGrid.ItemsSource which is not possible. Everything inside the DataTemplate will automatically have its DataContext set to an item from the above mentioned collection, eg. each data item will be set as the DataContext in each DataTemplate when it is rendered.

Therefore, for your current code to work, your data items in that collection should also have CurrentItem and GridVal properties, but I'm guessing that they don't. You should also have errors in your Output Window in Visual Studio that clearly tell you that there is no CurrentItem property in whatever object type is in your collection.

I suggest that you read through the Data Templating Overview‎ page on MSDN to help you better understand the situation.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Although the second `CurrentItem` is included in the `DataTemplate`, so at least they are not binding to the same `DataContext` (unless that's been modified somewhere outside the code example). However, his explanation does seem to point to that he is inspecting the "parent" property, not the child property. – default Mar 03 '15 at 14:01
  • @Sheridan Can you explain with example – Rohit Mar 03 '15 at 14:08
0

You can use binding with Relative source:

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

Or with specified ElementName:

{Binding Path=DataContext.PathToProperty, ElementName=myListBox}
Community
  • 1
  • 1
Miłosz Wierzbicki
  • 2,082
  • 1
  • 13
  • 15