Hello,
I have the following XAML and the BackgroundActive property in my ViewModel gets read, but doesn't seem to get set in the datagrid. If I hardcode a brush color to the BackgroundActive value, it displays as it should. Any help much appreciated.
<igDP:XamDataGrid Name="DocumentsGrid" DataSource="{Binding Documents}" ActiveDataItem="{Binding SelectedRow}" RecordLoadMode="{Binding GridRecordLoadMode, Mode=OneTime}" RecordContainerGenerationMode="{Binding ContainerGenerationMode, Mode=OneTime}">
<igDP:XamDataGrid.Resources>
<Style TargetType="{x:Type igDP:DataRecordCellArea}">
<Setter Property="BackgroundActive" Value="{Binding DataContext.ActiveGridRowColor, ElementName=DocumentsGrid, Mode=OneWay}"/>
</Style>
</igDP:XamDataGrid.Resources>
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings AllowEdit="False" />
</igDP:XamDataGrid.FieldSettings>
</igDP:XamDataGrid>
One solution is to use a DataTrigger in a CellValuePresenterStyle.
Try the following:
<Style TargetType="{x:Type igDP:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding Record.IsActive, RelativeSource={RelativeSource Self}}" Value="True"> <Setter Property="BackgroundActive" Value="{Binding Path=ActiveGridRowColor, Source={StaticResource myDataSource}}" /> </DataTrigger> </Style.Triggers></Style>
This is declared as a global style. You can give this style a Key and assign it to all Fields by assigning the resource to the FieldLayout.FieldSettings.CellValuePresenterStyle or to individual Fields as well using the FieldLayouts.Field.FieldSettings.CellValuePresenterStyle.
I'm not sure how your data is set up. Is it safe to assume you have a property in your main data source called ActiveGridRowColor. And there is also a property called Documents. Since your XAML snippet doesn't include where the data is declared, I've added a Source declaration which assumes a data source declared in a resource with key myDataSource. If this isn't enough to get you going, send more info about the data so I might make sure the binding expression works.
Thank you!
Thanks for your reply Curtis, I tried using your implementation and it doesn't seem to work.
I tried doing it globally with a key. I removed the Source binding for myDataSource because I'm not sure what that is exactly (sorry for my noobishness, I usually just bind a ViewModel to a user control and that's my data source) and I assigned the style you provided to the FieldSettings CellValuePresenterStyle.
My DataContext (MyViewModel) for the user control does indeed have an ActiveGridRowColor and a Documents. In the code behind for my control, I just set the datacontext in the constructor: this.DataContext = new MyViewModel()
Documents is the data source for the grid and I'd like ActiveGridRowColor to be able to set the style's active background color for the selected row.
My previous implementation read the ActiveGridRowColor property, but the brush never got set...
ah, didn't notice that :) I changed it to CellValuePresenter, but now only the cell gets colored the way I wanted as opposed to the whole row. I want the whole active row to be colored. Also, the property still gets read multiple times, it'd be nice if there was a way to just read that property once. Progress is good though!
ActiveBackground only affects the active cell. There is another property for highlighting the entire row. I will finish that sample and post it for you. My sample colorizizes active cells and entire row backgrounds.
This may be a bit off topic, but how do I get and set the selected row's index through binding? I can't seem to figure it out and I basically pass the xamDatagrid into my ViewModel, but I still cant seem to get/set the selected row's index...
I think I figured it out, although I don't like having to injecf the xamDatagrid into my ViewModel, but I'm basically doing GetValue when my SelectedRow is being set through the Grid, then in there I'm reading the injected grid's ActiveRecord index.
For setting the active record, I'm doing the reverse by using SetValue.... I wish there was a more elegant way for this...something like a bindable "SelectedRowIndex" property on the xamDataGrid itself would be fantastic :)
Well, actually there is! If you are willing to wrap your data source in a CollectionView class, you can use the IsSynchronizedWithCurrentItem property in the XamDataGrid (orXamDataPresenter). The CollectionClass CurrentItem is the property that will represent the selected item from the grid.
I've attached a sample I already had that demonstrates this feature. The sample uses XamDataPresenter. However, it works with XamDataGrid as well.
And I'm still working on the Brush example.