Hello,
I have question on iterating records in XamDataGrid-
I have 3 columns in XamDataGrid- StaffCategory, StaffTypes and StaffDetails. Now, I want to iterate through 3rd column only and
if row1 value is same as row0 value in 3rd column then change value to NA or change cell value to empty.
if row2 value is same as row0 value in 3rd column then change value to NA or change cell value to empty.
and so on...
Could anyone please help me with some reference on iterating cell values in XamDataGrid?
CheersSagar
Hi Rob,
Thanks for sharing these details. You can close this thread now.
Cheers
Sagar
Hi Sagar,
For your first part, a DataRecordPresenter style should let you do this. You'll need a value converter, or maybe even a multi-value converter if you plan on allowing users to edit the cell values you are interested in. If editing won't be an issue then a value converter that takes in the DataContext of the DataRecordPresenter should be enough.
<Style TargetType="{x:Type igDP:DataRecordPresenter}"> <Setter Property="Background" Value="{Binding Converter={StaticResource RecordBackgroundConverter}}"/></Style>
public class RecordBackgroundConverter : IValueConverter{ public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var record = value as DataRecord; if (record.Cells[2].Value != record.Cells[3].Value) // column3 against column4 return new SolidColorBrush(Colors.Yellow); return new SolidColorBrush(Colors.Transparent); } ...}
Something like above will let you change the row background based on your criteria, assuming that you don't allow editing of the values. If you allow editing the above won't work because we are only listening for changes on the DataContext of the record presenter. Changing a value inside a cell is not going to trigger the converter to fire again. That's where a multi-value converter would come in. In the multi-value converter you would pass in the relevant cell values and then determine the appropriate background color.
<Style TargetType="{x:Type igDP:DataRecordPresenter}"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource RecordBackgroundConverter}"> <Binding Path="Cells[2].Value"/> <Binding Path="Cells[3].Value"/> <Binding Path="Cells[4].Value"/> ...etc </MultiBinding> </Setter></Style>
For your second criteria about the cell foreground, this can also be done through a style:
<Style TargetType="{x:Type igDP:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="NA"> <Setter Property="Foreground" Value="Red"/> </DataTrigger> </Style.Triggers></Style>
This style is binding to the CellValuePresenter's own Value property and checking if it equals "NA". If it does it will set the foreground color to red.
Hi Rob
My criteria are-
1. For a particular row, if column3 cell value does not match with column4 cell value then entire row should highlight with background color yellow. if column4 cell value does not match with column5 cell value then entire row should highlight with background color green etc.
2. In particular cell, if I found value as 'NA' then highlight with foreground color.
The appropriate way to change background/foreground colors on the row or individual cells is through a Style. Can you explain what particular criteria you are using to determine what color to use for the background/foreground of the cell? You will probably be able to use a DataTrigger in a Style for this.
This Cells[2].Value does work for me.
However, I am just wondering how to highlight this particular cell with color.
I tried this syntax - DataRecordPresenter.FromRecord(dr2).Background = Brushes.Aqua;
and it does shows Aqua color on first instance. but when I scroll up and down it does seem to change back to original white color.
Could you please suggest correct way to specify color in foreground and background?