Home Forums WPF controls Xceed DataGrid for WPF Coloring a single cell depending on the data

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #24888 |

    Hello.

    I want to do something, and i don’t find a way, although i have tried several things. I have a grid with these conditions:

    a) The cells of column A must have a different background color depending on the value inside them.
    b) The cells of column B must have a different background color depending on the value inside another cell of the same row.

    I have tried first to create a style with TargetType “DataCell” (something like in http://forums.xceed.com/forums/ShowPost.aspx?PostID=9797), but i can’t apply the coloring only to cells of one specific column.

    The better I have reached is to use CellContentTemplate, this way:

    <Window.Resources>
    <DataTemplate x:Key=”TemplateColumnA”>
    <TextBlock x:Name=”TextA” Text=”{Binding Path=.}” Background=”Red”></TextBlock>
    <DataTemplate.Triggers>
    <DataTrigger Binding=”{Binding Path=.}” Value=”Yes”>
    <Setter TargetName=”TextA” Property=”Background” Value=”Green”/>
    </DataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>
    </Window.Resources>

    (…)

    <xcdg:DataGridControl ItemsSource=”{Binding Source={StaticResource MySource}}”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column FieldName=”FieldA” CellContentTemplate=”{StaticResource TemplateColumnA}”/>
    <xcdg:Column FieldName=”FieldB”/>
    <xcdg:Column FieldName=”FieldC”/>
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl”>

    This have some problems:

    1) I don’t know how to do the case b), since the template is bound to a cell, not the row.
    2) I’m changing the background of the TextBlock inside the cell, but i’d like to change the cell itself (this is less important, but would be nicer).

    Any other idea? Thanks

    Imported from legacy forums. Posted by Andrés (had 791 views)

    Xceed Support
    Member
    Post count: 5658

    I suggest that you create a DataTemplateSelector that will return the correct CellContentTemplate according to whatever values you want. The selector should be assigned to each column’s CellContentTemplateSelector property.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 372 views)

    User (Old forums)
    Member
    Post count: 23064

    Can we get a real example of this in code? not xaml. It would probaly take about 6 lines of code at the most to show how this is done. ( I would hope! )

    Imported from legacy forums. Posted by David (had 387 views)

    User (Old forums)
    Member
    Post count: 23064

    I have an alternate solution for this issue, you can check it out in this site.

    http://ajhorus.wordpress.com

    In short, we handled any event that gets called for all Xceed.Wpf.DataGridContro.DataRow.

    From there you could validate against any value of any cell of any DataRow and set the cell property as you wish

    Imported from legacy forums. Posted by ajhorus (had 442 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for the solution, it seems to work and is not too difficult. However, I also found another solution, that can be completely made in XAML. I posted it <a href=”http://forums.xceed.com/forums/ShowPost.aspx?PostID=10529″>in this thread</a>, but I copy here because I think it’s important.

    Anyway, I strongly miss a “CellStyle” property of the data grid, this is a very common scenario and would be greatly simplified with it. Or at least, put an example of it in the grid’s documentation. Anyone of the Xceed staff here?

    This is my option. I discovered that the cell’s context is the corresponding item of the items source (in my case a DataRow), and I learned to use RelativeSource. With it, it’s easy to solve cases a) and b), since I have access to the whole row.

    <Style TargetType=”{x:Type xcdg:DataCell}”>
    <Style.Triggers>
    <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
    <Condition Binding=”{Binding RelativeSource={x:Static RelativeSource.Self},Path=FieldName}” Value=”MyColumn”/>
    <Condition Binding=”{Binding Path=MyColumn}” Value=”Yes”/>
    </MultiDataTrigger.Conditions>
    <Setter Property=”FontSize” Value=”15″/>
    <Setter Property=”Background” Value=”Red”/>
    </MultiDataTrigger>
    </Style.Triggers>
    </Style>

    Imported from legacy forums. Posted by Andrés (had 4708 views)

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