Home Forums WPF controls Xceed DataGrid for WPF Custom Cell

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

    I have an application that uses unbound data, in which I add columns programmatically, i.e.

    view.ItemProperties.Clear();
    view.ItemProperties.Add( new DataGridItemProperty( “FirstName”, “FirstName”, typeof( string ) ) );
    view.ItemProperties.Add( new DataGridItemProperty( “LastName”, “LastName”, typeof( string ) ) );
    view.ItemProperties.Add( new DataGridItemProperty( “Occupation”, “Occupation”, typeof( Occupation ) ) );

    How would I make a column that contained a rectangle for each row that was filled with a color based on what the value of one of my properties (i.e. field in my row data structure) was? Like if fieldx were 0, it would be colored red, if fieldx were 1, it would be colored yellow, and so on. I assume it’s some sort of data template but I’m having trouble with the syntax. Thanks.

    Imported from legacy forums. Posted by Brad (had 4873 views)

    Xceed Support
    Member
    Post count: 5658

    You could try using the CellContentTemplate and CellContentTemplateSelector properties on the column whose cell values you want to color depending on the cell value.

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

    User (Old forums)
    Member
    Post count: 23064

    In XAML? Since my columns are defined in code and my data is unbound, how would I add a CellContentTemplate?

    Imported from legacy forums. Posted by Brad (had 290 views)

    Xceed Support
    Member
    Post count: 5658

    Although the DataGridItemProperties identify the columns that will end up in the grid, you can still access the actual columns through the grid’s Columns collection (after the grid has been bound). For example:

    grid.Columns[ “ShipCountry” ].CellContentTemplateSelector = new CellSelector();

    To provide a custom DataTemplateSelector, a class that derives from the DataTemplateSelector and override the SelectTemplate method that will return the appropriate DataTemplates.

    Hope this helps.

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

    User (Old forums)
    Member
    Post count: 23064

    What I’ve done so far is this:

    C#:
    ———-

    grid.Columns[ “ShipCountry” ].CellContentTemplate = this.FindResource( “myDataTemplate” ) as DataTemplate;

    XAML:
    ———-

    <DataTemplate x:Key=”myDataTemplate”>
    <Rectangle Height=”16″ Width=”16″/>
    <DataTemplate.Triggers>
    <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
    <Condition Binding=”{Binding ShipCountry}” Value=”0″/>
    </MultiDataTrigger.Conditions>
    <Setter Property=”Rectangle.Fill” Value=”Red”/>
    </MultiDataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>

    The C# part does assign the CellContentTemplate, but the XAML part obviously doesn’t work because I don’t know how to fill the rectangle based on the value of “ShipCountry”. If it’s 0, I want the rectangle red, etc. Do you see what I am trying to do?

    Imported from legacy forums. Posted by Brad (had 267 views)

    Xceed Support
    Member
    Post count: 5658

    The content of the cell becomes the DataContext of the DataTemplate. This means that if you are creating a CellContentTemplate for the “ShipCountry” column, from within the DataTemplate a simple “{Binding}” should resolve to the Cell’s value…

    e.g.:

    <DataTemplate x:Key=”myDataTemplate”>
    <Rectangle x:Name=”myRect” Height=”16″ Width=”16″/>
    <DataTemplate.Triggers>
    <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
    <Condition Binding=”{Binding}” Value=”0″/>
    </MultiDataTrigger.Conditions>
    <Setter TargetName=”myRect” Property=”Fill” Value=”Red”/>
    </MultiDataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 240 views)

    User (Old forums)
    Member
    Post count: 23064

    Interesting… didn’t know about “{Binding}”. Thanks!

    Imported from legacy forums. Posted by Brad (had 309 views)

    Xceed Support
    Member
    Post count: 5658

    {Binding} is the same as {Binding Path=.}

    Imported from legacy forums. Posted by Marcus [Xceed] (had 6095 views)

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