Welcome to the Xceed Community | Help
Community Search  
More Search Options

DataGrid Cell styling based on condition

Sort Posts: Previous Next
  •  03-23-2009, 12:02 PM Post no. 19530

    DataGrid Cell styling based on condition

    I have a problem where we need to set style on WPF Xceed grid cell, where we binding grid thru custom data source say DataModelA. Now datamodal have 10 properties. Where we want to set style on column 5 based on value of column 3.

    Say

    column 3 has value < 10 column 5 of similar row should be in bold

    column 3 has value > 10 column 5 of similar row should be in Italic

    column 3 has value = 10 column 5 of similar need to show animation

     

    Now I tried fist using DataTrigger but DataTrigger not supports condition like >,<,= and we also not able set other cells value.

    I tried to found Item bound event, but could not locate one.

    I tried to think with some rule set where rule get fired, we associate a event with it and can check\process that row.

    I tried with some event triggered, but that is not succeded.

    I prefer to do coding approach instead of XAML, as it gives more control over logic.

     

    Any one have idea about this?

     

     

     

     

  •  03-25-2009, 9:49 AM Post no. 19619 in reply to 19530

    Re: DataGrid Cell styling based on condition

    The DataTrigger does not support those types of conditions; however, a DataTrigger does support converters, which is what I suggest you use. In the converter, you can apply the logic you want and return a  boolean value indicating if the criteria are met.
    Senior Technical Writer
    - Xceed Software

    In three words I can sum up everything I've learned about life: it goes on.
  •  04-13-2009, 12:27 PM Post no. 20157 in reply to 19619

    Re: DataGrid Cell styling based on condition

    Thanks,

     

    I have created property say "IsValid" So I do all calculation in that property and return true or false. Now based on this property I want to set style or color on another column sourceFieldName.

    So I have created a template and textblock and binding it to SourceFieldName where based on RelativeSource I am tryting to find object Model. And then want to check property "IsValid".

    Now based on this property want to set TextBlock. But code seems unable to work but I know logically it correct. So could you please help me out where I am missing?

     

    Here I am binding grid Data Source as IList<Model>.

    public static void SetConditionalFormatingOnCell(Xceed.Wpf.DataGrid.DataGridControl grid, string sourceFieldName, string formatFieldName, object value, System.Windows.Media.Color color)

    {

    if (grid != null)

    {

    if (grid.Columns[sourceFieldName] != null && grid.Columns[formatFieldName] != null)

    {

    Xceed.Wpf.DataGrid.Column column = grid.Columns[sourceFieldName];

    System.Windows.Data.Binding binding = new System.Windows.Data.Binding();

    FrameworkElementFactory textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));

    textBlockFactory.Name = "formatedTextBlock" + sourceFieldName;

    textBlockFactory.SetBinding(TextBlock.TextProperty, binding);

    textBlockFactory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Left);

    DataTemplate template = new DataTemplate();

    DataTrigger trigger = new DataTrigger();

    System.Windows.Data.Binding binding1 = new System.Windows.Data.Binding();

    binding1.RelativeSource = new System.Windows.Data.RelativeSource(System.Windows.Data.RelativeSourceMode.FindAncestor);

    binding1.RelativeSource.AncestorType = typeof(Model);

    binding1.Path = new PropertyPath("IsValid");

     

    Style style = new Style();

    trigger.Binding = binding1;

    trigger.Setters.Add(new Setter(TextBlock.BackgroundProperty, new System.Windows.Media.SolidColorBrush(color)));

    trigger.Value = "false";

    //style.Triggers.Add(trigger);

    template.VisualTree = textBlockFactory;

    column.CellContentTemplate = template;

    column.CellContentTemplate.Triggers.Add(trigger);

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.