Home Forums WPF controls Xceed DataGrid for WPF Foreground text in DataGridControl cell

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • manubust
    Participant
    Post count: 3
    #43528 |

    Hello guys,
    First of all, thank you for your help, I was trying to solve this myself but after several days I just gave up. Here below my problem:

    I’m using a DatagridControl to show a list of machines, I want the foreground color text of the columnn corresponding to the machine size be red if it exceeds a certain value (I have a property called “IsSizeHigherThanLimit” in my object Machine).

    I tried declaring this in my resource dictionary:

    
    <Style x:Key="DataGridMachineSizeStyle" TargetType="{x:Type xcdg:DataCell}">
       <Style.Triggers>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=xcdg:DataRow, AncestorLevel=1}, Path=IsSizeHigherThanLimit}" Value="true">
             <Setter Property="Foreground" Value="Red" />
          </DataTrigger>            
       </Style.Triggers>
    </Style>

    My datagrid column :

    <xcdg:Column x:Name="MachineSize" Title="MachineSize" FieldName="MachineSize" ReadOnly="True" Width="65" />

    Actually I dont know how to call the style I defined in my Resource dictionary, I cannot find the proeprty “Style” in xcdg:Column…

    I have also tried declaring a CellContentTemplate as following:

    
    <xcdg:Column x:Name="MachineSize" Title="MachineSize" FieldName="MachineSize" ReadOnly="True" Width="65" >
      <xcdg:Column.CellContentTemplate>
         <DataTemplate>
            <TextBlock Text="{Binding MachineSize}" Foreground="{Binding IsSizeHigherThanLimit, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource BoolToForegroundConverter}}"/>
         </DataTemplate>
      </xcdg:Column.CellContentTemplate>
    </xcdg:Column>

    My last test was to use the property TextBlock.Foreground, but even that it didnt work, I was just trying to make work basic things (without binding):

    
    <xcdg:Column x:Name="MachineSize" Title="MachineSize" FieldName="MachineSize" ReadOnly="True" Width="65" TextBlock.Foreground="Red" >
    

    Some ideas?

    I really would appreciate a little of help.

    Thank you in advance,
    Manuel

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi manubust,

    You can set a Style on DataCell to change the Foreground color.

    It would use 2 conditions, one to make sure the Style only applies to the MachineSize column, and the other to check if the size limit is exceeded.

    For example:

    
       <Style TargetType="{x:Type xcdg:DataCell}">
          <Style.Triggers>
             <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                   <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FieldName}"
                              Value="MachineSize" />
                   <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}, Path=DataContext.IsSizeHigherThanLimit}"
                              Value="true" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Red"/>
             </MultiDataTrigger>
          </Style.Triggers>
       </Style>
    manubust
    Participant
    Post count: 3

    Thank you Diane for your answer,

    I dont understand when you say : “You can set a Style on DataCell to change the Foreground color.”
    In which component? <xcdg:DataGridControl> ? <xcdg:Column> ?

    I tried to find a style property in <xcdg:Column> but I did not manage to find it. I need to apply this color rule only for one column..

    Thank you in advance,

    Diane [Xceed]
    Moderator
    Post count: 1353

    The Style is set in your resources, either at the DataGridControl level, or Page/Window level. Below are examples of both of these. You can also set it on any parent’s resources in between if that suites your needs better.

    <Window.Resources>
        <xcdg:DataGridCollectionViewSource x:Key="mySource" ... />
        <Style TargetType="{x:Type xcdg:DataCell}">
            [...]
        </Style>
    </Window.Resources>
    <xcdg:DataGridControl x:Name="myGrid" ... >
        <xcdg:DataGridControl.Resources>
            <Style TargetType="{x:Type xcdg:DataCell}">
                [...]
            </Style>
        </xcdg:DataGridControl.Resources>
    </xcdg:DataGridControl>
    manubust
    Participant
    Post count: 3

    Thanks a lot Diane,

    It works perfectly!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.