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
490
CellValuePresenter style is being overridden when a new field is created with its own style
posted

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

Parents
No Data
Reply
  • 34510
    Verified Answer
    Offline posted

    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.

Children