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...
Hi,
OK, in that case, change the binding expression in the Style to match what you had originally. Another approach would be to instantiate the ViewModel in the Resource of the Window or UserControl and assign the DataContext in the XAML. The advantage of that approach is then you will see the data rendered in Visual Studio and Blend design spaces for the XAML file and for when the XAML is rendered in another XAML file (which would be the case for a UserControl). Regardless, the following should now work:
<Style TargetType="{x:Type igDP:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding Record.IsActive, RelativeSource={RelativeSource Self}}" Value="True"> <Setter Property="BackgroundActive" Value="{Binding Path=DataContext.ActiveGridRowColor, ElementName=DocumentsGrid}" /> </DataTrigger> </Style.Triggers></Style>
That didn't seem to work... I tried making it part of the UserControl.Resoures with a key and also as part of the data grid as follows:
<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:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding Record.IsActive, RelativeSource={RelativeSource Self}}" Value="True"> <Setter Property="BackgroundActive" Value="{Binding Path=DataContext.ActiveGridRowColor, ElementName=DocumentsGrid}"/> </DataTrigger> </Style.Triggers> </Style>
</igDP:XamDataGrid.Resources> <igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings AllowEdit="False"/>
</igDP:XamDataGrid.FieldSettings> </igDP:XamDataGrid>
How odd, it works for me. OK. So I can send you a working sample and you can compare what I did with your code or you can create a sample project that resembles your code and I will make it work in your sample. Whatever is easiest for you!
I think I see what's going on... in the new sample project I created, I prepopulate the Documents datatable in the ViewModel constructor. ActiveGridRowColor properly reads from my property.
In my project where it doesn't work, I don't prepopulate the Documents datatable as it's populated by an event handler. ActiveGridRowColor doesn't get read and set this way... Is there any way to get the desired result without prepopulating the Datagrid's data source?
Well it seems I've gotten the ActiveGridRowColor property to read, however it's not setting the brush color from that property in my ViewModel. Right now I have it returning Brushes.Blue, but it seems that if it reads from my property it becomes transparent...Basically I'm back to square one :)
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.
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 :)
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...
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.
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!