问题
I have a WPF Caliburn.Micro application. I used to have there a DataGrid, and here is a part of the code:
<DataGrid x:Name="FirstEntries" Grid.Row="5"
AutoGenerateColumns="False"
BaseControls:DataGridExtension.Columns="{Binding FirstEntryGridColumns}"
CanUserAddRows="False" IsReadOnly="True"
SelectedItem="{Binding Path=SelectedFirstEntry}">
<DataGrid.Resources>
<conv:StatusToBackgroundColorConverter x:Key="StatusToBackgroundColor"/>
</DataGrid.Resources>
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Path=Status, Converter={StaticResource StatusToBackgroundColor}}"></Setter>
<Setter Property="cal:Message.Attach" Value="[Event MouseDoubleClick] = [Action OnDoubleClickFirstEntry($dataContext)]"/>
</Style.Setters>
</Style>
</DataGrid.ItemContainerStyle>
You can see that each row's background color is bound to Status field value, and a double-click event is handled. Now I am migrating to ComponentOne's FlexGrid, and I don't know how I can achieve the same there, as FlexGrid doesn't seem to know ItemContainerStyle.
Could you please help me with this? Thanks.
回答1:
C1 FlexGrid does things a little "WinFormsy" for performance reasons and doesn't utilize DependencyProperties, or styles/templates, so you cannot use data triggers to set the row background or set a command to an event like you desire. Their suggestion is to use the Cell's mouseclick events to handle it all in code.
My suggestion, if at all possible, is to go back to WPF 4.0's DataGrid and bind to an ICollectionView to utilize it's Filtering function. Linked are many of Bea Stollnitz' tutorials on manipulating collection views.
回答2:
Have you had a look at the CellFactory class and ICellFactory interface. I used this to set different backgroundcolors depending of the item status in one of my projects.
Public Overrides Sub CreateCellContent(grid As C1.WPF.FlexGrid.C1FlexGrid, bdr As Border, rng As C1.WPF.FlexGrid.CellRange)
MyBase.CreateCellContent(grid, bdr, rng)
Dim infPre As InfPresenterTextEntity
infPre = CType(grid.Rows(rng.Row).DataItem, InfPresenterTextEntity)
If Not infPre Is Nothing Then
If infPre.IsNew Then
grid.Rows(rng.Row).Background = Brushes.LightGreen
ElseIf infPre.IsDirty Then
grid.Rows(rng.Row).Background = Brushes.LightYellow
End If
'grid.AutoSizeRow(rng.Row, 0)
'grid.AutoSizeRows(rng.Row, rng.Row, 0)
End if
End Sub
来源:https://stackoverflow.com/questions/11852418/componentones-flexgrid-background-color