Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1170
CellValuePresenter ContentStringFormat Binding
posted

I want to add a setting to my application that will let users specify the format of a DateTime column on the fly. I added a trigger to my CellValuePresenter style:

<Style.Triggers>
    <DataTrigger Binding="{Binding Path=Field.Label, RelativeSource={RelativeSource Self}, Converter={StaticResource TimeStringFormatConverter}}" Value="True">
        <Setter Property="ContentStringFormat" Value="{Binding Path=TimeStampFormat, Source={StaticResource Settings}}"/>
    </DataTrigger>
</Style.Triggers>

The converter returns true when the label matches a list of DateTime column names. This works for the most part, except that when I change the setting the rows in view are not immediately updated with the new ContentStringFormat. If I scroll down the previously hidden rows have the right format, and then if I scroll back up the original rows are now displayed correctly.

What would be the best way to force those cells in view to be repainted? I tried modifying the FieldLayouts collection of the XamDataGrid, since that worked for a similar issue I had when changing the foreground color, but that code doesn't have any effect on the ContentStringFormat it seems.

  • 34510
    Verified Answer
    Offline posted

    Hello John,

    The main issue here is that this trigger only fires when the Field.Label property is updated.  For example, if the user changes the time stamp format through some outside control, that isn't going to fire a property changed notification for the Field.Label so the trigger isn't going to do anything for cells that are already in view.  When you scroll down however, since the grid virtualizes the cells, cells from the top rows now off screen have been moved to cells at the bottom that are coming into view.  Their data context changes to the new DataRecord and the triggers are fired to make sure things are up to date.  Since the trigger fires your ContentStringFormat gets updated and those cells look correct.

    One thing you could do to force the trigger to fire is to force a property changed notification to occur on the field's Label property.  When the user selects a new format, you can detect this and force the label to update by resetting it.  You might need to set it to an empty string first before reassigning it.

    The other option is to assign the ContentFormatString directly to the TimeStampFormat property via a binding so that when the user updates this property, the ContentFormatString is updated.  You might be able to use a MultiValueConverter here to pass in the Field so you can check if you really should update the ContentFormatString.

    Let me know if you have any questions on this.