Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
495
Capture SelectedDataItems from dataGrid and use them to populate another viewModel
posted

Hi, So I have a datagrid, bound to BindingList<Instrument> from my viewmodel like so:

 

<Grid>

<igWPF:XamDataGrid Name="SecurityBlotterGrid"

 Height="Auto"

  DataSource="{Binding InstrumentList}"

ExecutingCommand="SecurityBlotterGrid_ExecutingCommand"

  SelectedDataItems ="{Binding selectedInstrumentList, Mode=TwoWay}"

SelectedDataItemsScope ="RecordsOnly">

<igWPF:XamDataGrid.Resources>

<Style TargetType="{x:Type igWPF:CellValuePresenter}">

<Setter Property="BorderThickness" Value="0,0,1,1"/>

<Setter Property="BorderBrush" Value="Transparent"/>

<Setter Property="BorderHoverBrush" Value="Transparent"/>

</Style>

</igWPF:XamDataGrid.Resources>

<igWPF:XamDataGrid.FieldLayoutSettings>

<igWPF:FieldLayoutSettings AllowClipboardOperations="All"

 CopyFieldLabelsToClipboard="True"

 HeaderPrefixAreaDisplayMode="FieldChooserButton"

 AutoGenerateFields="False"

 SummaryEvaluationMode="Auto"

FilterUIType="FilterRecord"

SelectionTypeRecord="Extended"/>

</igWPF:XamDataGrid.FieldLayoutSettings>

<igWPF:XamDataGrid.FieldSettings>

<igWPF:FieldSettings AllowRecordFiltering="True"

AllowSummaries="true" SummaryUIType="MultiSelect"

CellClickAction="SelectRecord"/>

</igWPF:XamDataGrid.FieldSettings>

<igWPF:XamDataGrid.FieldLayouts>

<igWPF:FieldLayout IsDefault="True">

<igWPF:FieldLayout.Fields>

[...]

and then closing tags for all.

 

 

Now, the grid populates just fine, but what I need to be able to do is have the user select one ore more records from the grid, and use those records to determine the data that shows up on a completely different viewModel.

 

The cleanest way I thought to achieve this was to use the part that says SelectedDataItems={Binding selectedInstrumentList, mode=TwoWay}"

 

and of course I have a selectedInstrumentList in my viewmodel of type BindingList<Instrument>

 

yet when I try to do this:

selectedInstrumentList.ListChanged += new ListChangedEventHandler(selectedInstrumentList_ListChanged);

 

the ListChangedEvent never occurs.

 

 

Next I tried capturing it from the codebehind which is kind of sloppy, but I want to get it done:

SecurityBlotterGrid.SelectedItemsChanged += new EventHandler<Infragistics.Windows.DataPresenter.Events.SelectedItemsChangedEventArgs>(SecurityBlotterGrid_SelectedItemsChanged);

 

void SecurityBlotterGrid_SelectedItemsChanged(object sender, Infragistics.Windows.DataPresenter.Events.SelectedItemsChangedEventArgs e)

        {

           

var grid = (XamDataGrid)sender;

           

var instrumentArray = grid.SelectedDataItems;

           

if (instrumentArray != null)

            {

               

var tempBinding = new BindingList<InstrumentSummary>();

                ViewModel.selectedInstrumentList.Clear();

               

foreach (var instrument in instrumentArray)

                {

ViewModel.selectedInstrumentList.Add((

InstrumentSummary)instrument);

                }

            }

        }

 

 

but grid.SelectedDataItems seems to always have the previous selected item, not the one(s) i'm currently trying to select.  Furthermore I have to do viewModel.selectedInstrumentList.Add((InstrumentSummary)instrument) one-at-a-time because if I try to do it en masse, the listchanged event doesn't fire on the viewmodel. 

 

can you please explain to me what I am doing wrong?

Thanks