I need help using the CellValuePresenter with the xamDataGrid. I am setting a style in my Grid.Resources like this:
<igDP:XamDataGrid.Resources>
<Style TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Background">
<Setter.Value>
<Binding Converter="{StaticResource bconv}" RelativeSource="{RelativeSource self}" Path="Field.Name" />
</Setter.Value>
</Setter>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<EventSetter Event="MouseLeave" Handler="CellPresenter_MouseLeave"/>
<EventSetter Event="MouseEnter" Handler="CellPresenter_MouseEnter"/>
</Style>
</igDP:XamDataGrid.Resources>
The details here are not too important other than the fact that it works as expected. The issue occurs when I add a new unbound field dynamically through code and need to change that field’s settings based on parameter only present during runtime. I’m doing this through a CellValuePresenterStyleSelector like this:
field.Settings.CellValuePresenterStyleSelector = new CVPStyleSelector_Field(RegularStyle, EditedStyle);
where I decide which style to use based on values in my Style Selector. This basically only deals with how the label itself is formatted. Again this works well, except it overrides my previous CellValuePresenter. I tried adding the same properties as I had in my first Style but this would only set the style on the label of the cell and not the entire cell as required. For example the background was only set behind the label but not filling the entire cell. I got closer by changing my background and mouse events into a CellPresenter instead of the CellValuePresenter but I still wasn’t able to get the style as required.
Is there a way to have a CellValuePresenter set for the entire grid and then have a different CellValuePresenter set for just the label but not override the first style?
Thanks
Hi mbhydro,
If I understand this correctly, you want to apply the style inside your XamDataGrid.Resources to all the cells in the grid and when you add an UnboundField dynamically in code, you want the cells of that field to also use the style along with the style selected by the StyleSelector. Is this correct?
For this kind of requirement you're going to need to create styles that are based on other styles. Here is an example of what I mean.
First we take the style you provided above and give it a key.
<Style x:Key="DefaultCellStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Background"> <Setter.Value> <Binding Converter="{StaticResource bconv}" RelativeSource="{RelativeSource self}" Path="Field.Name" /> </Setter.Value> </Setter> <Setter Property="HorizontalContentAlignment" Value="Right"/> <EventSetter Event="MouseLeave" Handler="CellPresenter_MouseLeave"/> <EventSetter Event="MouseEnter" Handler="CellPresenter_MouseEnter"/> </Style>
This style is going to be the default style we apply to all cells in the grid. Since we gave the style a key it's no longer going to apply itself automatically. So let's apply it manually.
<igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings CellValuePresenterStyle="{StaticResource DefaultCellStyle}"/> </igDP:XamDataGrid.FieldSettings>
Now all of our cells will use the DefaultCellStyle when they are created. For any cells that you want a special style applied but you want to keep the default style as well, that special style needs to be created based on the default one.
<Style x:Key="EditedCellStyle" BasedOn="{StaticResource DefaultCellStyle}" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Foreground" Value="Red"/> </Style>
Now you can add a field dynamically and set it's CellValuePresenter style to the SpecialCellStyle and that field will not only still have the same settings as the default style but it will have a red foreground as well indicating to you that the special style is being used. This means that the cells for the dynamically added field will still handle the MouseLeave and MouseEnter events just like normal cells. This works fine with a StyleSelector as well. Just make sure that any style returned by the style selector is a style based on DefaultCellStyle.
Let me know if you have any questions on this.
Thank you. This is exactly what I’m trying to do. It’s almost working now except I’m still getting the issue were it’s applying the background and mouse events to only the label and not the entire cell. In the screenshot below the 7:00 column should be all blue but for some reason it’s only changing the background of the label and not the entire cell.
Aha! That explains it then. The reason HorizontalAlignment was causing that issue is because it sets the alignment of the cell itself. In this case you were setting HorizontalAlignment to Right which actually shrinks the CellValuePresenter towards the right. You'd only every see this if you set the background color like you did. If you did not set the color you wouldn't see a difference between the CellValuePresenter and the area it's supposed to occupy even though the CellValuePresenter is technically aligned to the right. The colors would have matched and you wouldn't see a seam.
For the most part, you want to leave HorizontalAlignment set to Stretch and just set the HorizontalContentAlignment instead which will apply to the underlying control that displays the value.
I'm glad you were able to resolve your issue. Let me know if you had any other questions on this matter.
Hi,
I have resolved my issue. The problem was happening since I was using HorizontalAlignment instead of HorizontalContentAlignment. Apparently if you have both the background property and Horizontal Alignment set in the same style the background will only be set to the label.
I’m not sure why this was happening but it was the Horizontal Content Alignment I was meaning to set in the first place.
Thank you for all your help!
Here is a simplified version of my solution. We are using an entity framework which is not included but you should be able to see how it’s working without this.
Thank you for your help!
Can you send me the application you're working with so I can debug it? Based on what I see in the screenshot the issue seems to be with the styles.