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
155
Hoe to handle combobox selection changed within xamdatagrid
posted

I have xamdatagrid and within this grid there is a column which is of type combobox. Now based upon certain condition when I change the selection of the comobobox a new record should get added within the Grid. Please suggest if I need to add trigger to the combobox upon selectionchanged and raise and event in that case I would need the index of currently grid row selected and index of combov=box selected item.

Please advise how can achive the above.

Below is the xaml code:

<UserControl x:Class="DLVR.IndividualPositionReport.Visualisation.Views.AggreagteCalcParamCollectionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:local="clr-namespace:DLVR.IndividualPositionReport.Visualisation.Views"
xmlns:bmg="clr-namespace:DLVR.IndividualPositionReport.Visualisation.BenchmarkGrid"
xmlns:bm="clr-namespace:DLVR.IndividualPositionReport.Visualisation.CompensationBlock"
xmlns:vp="clr-namespace:DLVR.Shared.BaseClasses.VisualisationParameters;assembly=DLVR.Shared"
xmlns:idp="http://infragistics.com/DataPresenter"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ig="http://infragistics.com/Editors"
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.Resources>
<Style x:Key="textField" TargetType="{x:Type idp:CellValuePresenter}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=DataContext.DataItem.Value.Parameters,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type idp:CellValuePresenter}}}" DataContext="{Binding Value,Mode=OneWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction CommandParameter="{Binding Path=DataContext.DataItem}" Command="{Binding DataContext.SelectChangeCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type idp:CellValuePresenter}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ig:XamTextEditor Value="{Binding Value}">
<ig:XamTextEditor.Template>
<ControlTemplate>
<TextBlock Text="{Binding Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</ControlTemplate>
</ig:XamTextEditor.Template>
<ig:XamTextEditor.EditTemplate>
<ControlTemplate>
<TextBox Text="{Binding Value, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
</ControlTemplate>
</ig:XamTextEditor.EditTemplate>
</ig:XamTextEditor>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel> 
</ItemsControl>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="combo" TargetType="{x:Type idp:CellValuePresenter}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.DataItem.Options,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type idp:CellValuePresenter}}}"
DisplayMemberPath="Label"
SelectedItem="{Binding Path=DataContext.DataItem.Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type idp:CellValuePresenter}}}">

</ComboBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Add Row" Width="100" Margin="0,0,20,0" Command="{Binding AddNewRowCommand}"></Button>
<Button Content="Delete Row(s)" Width="100" Command="{Binding DeleteRowCommand}"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<CheckBox IsChecked="{Binding IsVarianceChecked}" Width="Auto" Command="{Binding VarianceCommand}"/>
<TextBlock Text="Show variance from Sponsor" Width="Auto"/>
</StackPanel>
<idp:XamDataGrid x:Name="td" Grid.Row="2" 
DataSource="{Binding FilteredCalcParameter,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedDataItems="{Binding SelectedRows,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedDataItemsScope="RecordsOnly"
IsSynchronizedWithCurrentItem="True">
<idp:XamDataGrid.FieldLayoutSettings>
<idp:FieldLayoutSettings AutoGenerateFields="False"
></idp:FieldLayoutSettings>
</idp:XamDataGrid.FieldLayoutSettings>
<idp:XamDataGrid.FieldLayouts>
<idp:FieldLayout>
<idp:FieldLayout.Fields>
<idp:UnboundField BindingPath="Value" Label="Calculation" CellValuePresenterStyle="{StaticResource ResourceKey=combo}">
</idp:UnboundField>
<idp:UnboundField BindingPath="Value" Label="Parameter (where applicable)" BindingMode="TwoWay" CellValuePresenterStyle="{StaticResource ResourceKey=textField}">
</idp:UnboundField>
</idp:FieldLayout.Fields>
</idp:FieldLayout>
</idp:XamDataGrid.FieldLayouts>
</idp:XamDataGrid>
</Grid>
</UserControl>

  • 34810
    Offline posted

    Hello Richa,

    In this case, I would recommend that you do handle the SelectionChanged event, and you can do this using either the InvokeCommandAction EventTrigger structure that it appears you have placed on your ItemsControl, or you can use a Behavior<ComboBox> that hooks the SelectionChanged event. If you are unfamiliar with Behaviors, you can read about them here: https://wpftutorial.net/Behaviors.html.

    If you go the route of the InvokeCommandAction through an EventTrigger, I would probably recommend that you push the ComboBox itself into the CommandParameter. At the time that this fires, I would expect that the SelectedItem has already changed, and so you can detect the item that has changed using that property. You can get the actual index of the record by using the Infragistics.Windows.Utilities class and its static GetAncestorFromType method to get the overlaying CellValuePresenter from the ComboBox. The code for this would look like the following:

    CellValuePresenter cvp = Infragistics.Windows.Utilities.GetAncestorFromType(comboBox, typeof(CellValuePresenter), false) as CellValuePresenter;

    From the CellValuePresenter, you can get the underlying DataRecord and find out its index from the Record.Index property.

    If you go the route of the Behavior<ComboBox>, a similar procedure can be followed to get the CellValuePresenter and the Record index, and you can get the newly selected item from the event arguments of the SelectionChanged event, as in the Behavior<ComboBox> this would be handled directly.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer