I have a UserControl containing a xamDataGrid. The data source used for the grid is a sql data set. I can use different queries (and data sets) to fill the grid.
Now I should be able to change the background color for a specific rows, based on a value in a cell. Depending on the queries there are different cells. So I can't hard code the cell in the xaml code.I need to find a way, to set the cell and the logic that defines the background color outside the xaml file.
Unfortunately I don't have any idea how this could be achieved.
Any ideas how this could be done?
Markus
Hello Markus,
You can try the approach here.
Hope this helps.
Hallo Alex
Thank you very much for your hint. Sorry that I did not answer earlier. I meanwhile found a solutions based on your sample. For other users with similar problems I enclose a short description below.
Best Regards
I have several controls based on the same user control. The base user control contains a xamDataGrid. Depending on the type of the derived control there is different data (different structure) displayed in the grid.I want to change the background color for some rows. The color coding depends on the type of the derived control.
For each deived control, I have to provide the following:
The solution is not very elegant, but at least it works. The problem is, that the base user control has to be modified, when we need to change the background color of a new derived class.
Below you find the fragments of the relevant code
Converter:
class MyValueToColorConverter1 :IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return Binding.DoNothing; if (value is Int32) { Int32 Grade = (Int32)value; switch (Grade) { case 3: return Colors.Khaki; case 5: return Colors.GreenYellow; case 11: return Colors.Orange; default: return Binding.DoNothing; } } else { return Binding.DoNothing; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; } }
Styles for each derived control
<igDP:XamDataGrid.Resources> <Style TargetType="{x:Type igDP:DataRecordPresenter}" x:Key="BkgStyle1" > <Setter Property="Background" > <Setter.Value> <SolidColorBrush Color="{Binding Path=DataItem.Grade, Converter={StaticResource MyValueToColorConverter1 }}" Opacity="0.5" /> </Setter.Value> </Setter> </Style> <Style TargetType="{x:Type igDP:DataRecordPresenter}" x:Key="BkgStyle2" > <Setter Property="Background" > <Setter.Value> <SolidColorBrush Color="{Binding Path=DataItem.State, Converter={StaticResource MyValueToColorConverter2}}" Opacity="0.5" /> </Setter.Value> </Setter> </Style>
LayoutInitializing-Event Handler
private void xamMainDataGrid_FieldLayoutInitializing(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializingEventArgs e){ int LayoutIndex = xamMainDataGrid.FieldLayouts.IndexOf(e.FieldLayout); if (LayoutIndex == 0) { // // We assign styles for content control grid rows (Background color Coding) if (this is DerivedControl1) { Style BkgStyle = xamMainDataGrid.FindResource("BkgStyle1") as Style; e.FieldLayout.Settings.DataRecordPresenterStyle = BkgStyle; } else if (this is DerivedControl1) { Style BkgStyle = xamMainDataGrid.FindResource("BkgStyle2") as Style; e.FieldLayout.Settings.DataRecordPresenterStyle = BkgStyle; } }}