In the attached project I have a xamdatagrid with two combo box type controls on each row. When the value of one of the controls changes, I want to update the value of the other. For example, when the user uses the "Account number" control to select an account by account number, I want to update the "Account Name" control to refelect the selected account name. Please see the code below in MainWindow.xaml.cs:
private void SelectorTextBox_SelectionChanged(KeyValuePair<string, string> SelectedItem) { Trade t = (Trade)grid.ActiveDataItem; // Fails here because ActiveDataItem is null if row has not been committed. t.ACCT_NO = Convert.ToInt32(SelectedItem.Key); t.ACCT_Name = SelectedItem.Value; }
The problem only occurs if the user changes a control on the Add row, in which case the Active Data Item is null.
Thanks,
Sam
Hello Sam,
Thank you for your reply and for modifying the sample application. I have been looking into it and I have managed to reproduce the issue that you have described. The reason for the issue is that you custom SearchTextBox control is having OneWay for default Mode of the binding of the SelectedValue property. This cause the value of the editor, in which bound to the SelectedValue of the SearchTextBox, to not update when you type and the SelectedValue is changed. Since an item is added to the data source of the XamDataGrid, when a value of an editor of the AddNewRecord is changed, an item is not added to the source, which causes the DataItem property of the AddNewRecord to be null. You can avoid this issue, by setting the Mode of the Binding of the SearchTextBox to TwoWay as follow:
<Style TargetType="local:ControlHostEditor">
<Setter Property="ValueType" Value="{x:Type system:String}" />
<Setter Property="EditDataTemplate">
<Setter.Value>
<DataTemplate>
<samsControls:SelectorTextBox
SelectedValue="{Binding RelativeSource={RelativeSource Self}, Path=(local:ControlHostEditor.Editor).Value, Mode=TwoWay}"
SelectionChanged="SelectorTextBox_SelectionChanged"
ItemsSource="{Binding Path=(local:ControlHostEditor.Editor).Host.DataPresenter.DataContext.People, RelativeSource={RelativeSource Self}}"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SearchTermType="ID"
ResultsListWidth="400">
</samsControls:SelectorTextBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Please let me know if you need any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
Hi Krasimir, I (think) I fixed the example. In case it does not work the assembly you need is the lib directory.
I also modified the project to include the change you suggested. It still fails in the same place.
Thanks for your help,
Thank you for your reply. I have been looking into it and I could not managed to reproduce the issue that you have described. I have removed the check for the ActiveRecord property of the XamDataGrid, in the SelectionChanged event of the ComboBox and the approach is working for both AddNewRecord and the DataRecords of the XamDataGrid. I am attaching a video that shows the behavior that I am getting.
Would you please modify the sample application that I have attached with my previous reply, in order to show the scenario where, the ActiveRecord is null when you are editing a cell of the XamDataGrid, in order to be able to provide you with more accurate assistance on the matter?
Looking forward to hearing from you.
Krasimir, thank you for your reply. Unfortunately the method still fails when the user changes the combobox on the "Add" row.
I need to find a way to remove this line: if (grid.ActiveRecord != null)
This method needs to work whenever the user has access to the combobox even if it is the add row of the grid.
Thank you, Sam
Thank you for your post. I have been looking into it and I can suggest using the ActiveRecord of the XamDataGrid, instead of the ActiveDataItem. You can use the DataItem property of the DataRecord (the active record) in order to get the Trade object and then you can implement the logic that you need. Here is how you can use the ActiveRecord property of the XamDatatGrid.
private void SelectorTextBox_SelectionChanged(KeyValuePair<string, string> SelectedItem)
{
if (grid.ActiveRecord != null)
DataRecord record = grid.ActiveRecord as DataRecord;
Trade t = (Trade)record.DataItem;
t.ACCT_NO = Convert.ToInt32(SelectedItem.Key);
t.ACCT_Name = SelectedItem.Value;
}
I have modified the sample application that you have provided me with, in order to show this approach and change the SelectorTextBox control with a ComboBox, since the assembly that contains the control was not included into your attachment.