Hi there
I’m using WPF Infragistic 2011 V2 XamDatagrid. The application is based on MVVM. I need to make some of the records editable while the rest of the rows remain readonly based on a Boolean property (IsReadOnly) in my model. It appears to be such a simple task but I can’t find an example of how to do it without writing code behind. Please help.
Regards
Boris
Hi,
If your Boolean property is part of the same record, then you can use styles and triggers on individual cells to make them read-only based on the Boolean value. I am attaching a sample that demonstrates this.
The sample contains three columns one of them is holding Boolean value (check box) that controls the read-only state of the other two cells on a given row.
Let me know if this is what you were trying to aching.
Sam
There are some side effects:
1. Cells are not in edit mode when tabbing - have to click on each cell to enter edit mode
2. Grid Theme is lost. Do I have to repeat BorderBrash, Backgroung, etc. properties for each editor for each value of IsReadOnly field? (Four times in given example). What do I have to do if I need the grid to have IGTheme but still have some records to be readonly?
3. Unable to add new records to the grid if the last column has CellValuePresenterStyle set. In given example if you make “read only” field as field 0 and allow new records to be added you will have to press Enter twice to add a new record.
Any suggestions?
Hi Boris,
Can you attach the modified sample with your implementation so I can see the behavior?
Thanks,
Hi Sam
Run the sample, enter any value less than a 100 into Value field - observe both the Record and the Field level errors. Please note that Readonly check box has no effect.
Go to XAML and uncomment the following code:
<igDP:Field.Settings>
<igDP:FieldSettings CellValuePresenterStyle="{StaticResource ValueEditorStyle}"/>
</igDP:Field.Settings>
Readonly functionality is active now but Field level errors are not displayed
Cheers
Thank you for providing me with your implementation.
I used a different approach for data triggers. Instead of using CellValuePresenter, I used the editors for corresponding editors such as XamNumericEditor for Value and XamTextEditor for Label.
Please replace the control template that targets CellValuePresenter with the following Style I pasted below, and add one more for XamTextEditor. Also in the FieldSettings instead of CellValuePresenterStyle, use EditorStyle such as:
<igDP:FieldSettings EditorStyle="{StaticResource ValueEditorStyle}"/>
<Style x:Key="ValueEditorStyle"
TargetType="{x:Type igEditors:XamNumericEditor}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Cells[2].Value}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Cells[2].Value}" Value="False">
<Setter Property="IsEnabled" Value="True"/>
</Style.Triggers>
</Style>
Let me know how this worked for you. I tested it here, and it displayed the field errors as I would expect.
Thank you for your help. The grid works now as expected. For the benefit of other developers I'm uploading the sample with all the changes applied.
One question remains thou - How can I prevent read only rows from being deleted? But for this I'll start another thread with appropriate subject.
Thanks again
The simplist way that I can think of (in code behind) would be to handle the RecordDeleting event and cancel it.
private void xamDataGrid1_RecordsDeleting(object sender,
Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs e)
{
foreach (DataRecord dr in e.Records)
if ((bool)dr.Cells[2].Value == true)
e.DisplayPromptMessage = false;
e.Cancel = true;
e.Handled = true;
}
Would this work or you want to do it in XAML?
Unfortunatelly our only option is XAML