We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Change the Foreground color of a line in real time by Switch

Hi,

This is a simple code snippet changes the color of letters on the line by turning ON / OFF the switch installed in SfDataGrid, but it responds only to the first line. Even if Switch is changed from the second line onwards, the text color does not change. Please give me advice. The way without using MVVM is good for me, because MVVM is too difficult for me.


FoodProperty.cs

public class FoodProperty : INotifyPropertyChanged

{

  private bool flag;

  public string Id { set; get; }

  public string Name { set; get; }

 

  public bool Flag

  {

    set

    {

      flag = value;

      RaisePropertyChanged("Flag");

    }

    get => flag;

  }

 

  public event PropertyChangedEventHandler PropertyChanged;

  private void RaisePropertyChanged(string Name)

  {

    if (PropertyChanged != null)

    {

      this.PropertyChanged(this, new PropertyChangedEventArgs(Name));

    }

  }

}

MainPage.xaml.cs

public partial class MainPage : ContentPage

{

  private static ObservableCollection<FoodProperty> samples = new ObservableCollection<FoodProperty>();

  private SfDataGrid sfData = new SfDataGrid();

 

  public MainPage()

  {

    samples = new ObservableCollection<FoodProperty>

    {

      new FoodProperty{ Flag = true, Id = "06061", Name = "Cabbage" },

      new FoodProperty{ Flag = true, Id = "06153", Name = "Onions" },

      new FoodProperty{ Flag = true, Id = "06182", Name = "Tomatoes" },

      new FoodProperty{ Flag = true, Id = "06212", Name = "Carrot" },

      new FoodProperty{ Flag = true, Id = "06263", Name = "Broccoli" },

    };

 

    InitializeComponent();

 

    sfData.ItemsSource = samples;

 

    // Probably the problem lurks in this part.

    Style style = new Style(typeof(GridCell));

    style.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = new Binding("Flag", BindingMode.Default, new StyleConverter()) });

 

    sfData.Columns.Add(new GridSwitchColumn()

    {

      MappingName = "Flag",

      HeaderText = "On/OFF",

      CellStyle = style

    });

    sfData.Columns.Add(new GridTextColumn()

    {

      MappingName = "Id",

      HeaderText = "ID",

      CellStyle = style

    });

    sfData.Columns.Add(new GridTextColumn()

    {

      MappingName = "Name",

      HeaderText = "Name",

      CellStyle = style

    });

    Content = sfData;

  }

}

 

internal class StyleConverter : IValueConverter

{

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

  {

    bool _value = (bool)value;

    if (!_value)

    {

      return Color.Silver;

    }

    return Color.Black;

  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

  {

    return value;

  }

}


10 Replies

SP Subburaj Pandian Veluchamy Syncfusion Team March 11, 2019 10:19 AM UTC

Hi Akihiko, 
  
Thank you for contacting Syncfusion support. 
  
We have checked and we can able replicate the reported issue “Setting style in code behind is not updating properly” in Xamarin Forms. Currently, we are analyzing on the issue, we will validate and provide you the further details in two business days (March 13, 2019). We appreciate your patience until then. 
 
Regards,
Subburaj Pandian V  



AN Akihiko Nakama March 11, 2019 03:56 PM UTC

Hi Subburaj,


Thank you for your support.

I did not know whether it was a bug or my mistake.

I appreciate your continuous support.

Regards,

Akihiko Nakama



SP Subburaj Pandian Veluchamy Syncfusion Team March 12, 2019 10:09 AM UTC

Hi Akihiko, 
 
Thank you for the update. 
  
We are analyzing the root cause for the reported issue “Setting style in code behind is not updating properly” in Xamarin Forms and the run time changes is working on first data row alone. We need some more time to work on this probably three business days. We will validate and provide you the sample level solution or validated details within end of this week (March 15, 2019). We appreciate your patience until then. 
 
Regards,
Subburaj Pandian V  



AN Akihiko Nakama March 16, 2019 11:52 AM UTC

Hi Subburaj,

When I added UnboudRow to the DataGrid of the code snippet I sent the other day, the color of the text in the first row has not changed. When I turn off the switch, I want to set the text color of that line to gray and to exclude it from the sum. Conversely, when Switch turns on the OFF line, the character color of that line turns black and I want to be added them to the sum. I want to process them in real time. I wrote various code snippet, fixed it and tried it, but it doesn't work well. Please give me some advice on the code snippet or procedure that realizes such processing.

Regards,

Akihiko Nakama


FoodProperty.cs

  public class FoodProperty : INotifyPropertyChanged

  {

    private bool flag;

    public string Id { set; get; }

    public string Name { set; get; }

    public double Weight { set; get; }

 

    public bool Flag

    {

      set

      {

        flag = value;

        RaisePropertyChanged("Flag");

      }

      get => flag;

    }

 

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string Name)

    {

      if (PropertyChanged != null)

      {

        this.PropertyChanged(this, new PropertyChangedEventArgs(Name));

      }

    }

  }

 

MainPage.xaml.cs

  public partial class MainPage : ContentPage

  {

    private static ObservableCollection<FoodProperty> samples = new ObservableCollection<FoodProperty>();

    private SfDataGrid sfData = new SfDataGrid();

 

    public MainPage()

    {

      samples = new ObservableCollection<FoodProperty>

      {

        new FoodProperty{ Flag = true, Id = "06061", Name = "Cabbage", Weight = 12.3 },

        new FoodProperty{ Flag = true, Id = "06153", Name = "Onions" , Weight = 23.4},

        new FoodProperty{ Flag = true, Id = "06182", Name = "Tomatoes", Weight = 34.5 },

        new FoodProperty{ Flag = true, Id = "06212", Name = "Carrot", Weight = 45.6 },

        new FoodProperty{ Flag = true, Id = "06263", Name = "Broccoli", Weight = 56.7 },

      };

 

      InitializeComponent();

 

      sfData.ItemsSource = samples;

      sfData.AllowEditing = true;

      sfData.ColumnSizer = ColumnSizer.Auto;

 

      Style style = new Style(typeof(GridCell));

      style.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = new Binding("Flag", BindingMode.Default, new StyleConverter()) });

 

      sfData.Columns.Add(new GridSwitchColumn()

      {

        MappingName = "Flag",

        HeaderText = "On/OFF",

        AllowEditing=true,

        CellStyle = style

      });

      sfData.Columns.Add(new GridTextColumn()

      {

        MappingName = "Id",

        HeaderText = "ID",

        AllowEditing=false,

        CellStyle = style

      });

      sfData.Columns.Add(new GridTextColumn()

      {

        MappingName = "Name",

        HeaderText = "Name",

        AllowEditing = false,

        CellStyle = style

      });

      sfData.Columns.Add(new GridNumericColumn()

      {

        MappingName = "Weight",

        HeaderText = "Weight",

        AllowEditing = false,

        Format = "#,0.0",

        CellStyle = style

      });

      sfData.UnboundRows.Add(new GridUnboundRow() { Position = UnboundRowsPosition.FixedTop });

      sfData.QueryUnboundRow += SfData_QueryUnboundRow;

 

      Content = sfData;

    }

 

    private void SfData_QueryUnboundRow(object sender, GridUnboundRowEventArgs e)

    {

      int p = e.RowColumnIndex.ColumnIndex;

      if (e.UnboundAction == UnboundActions.QueryData)

      {

        if (p >= 3)

        {

          double s = 0.0;

          for (int i = 0; i < samples.Count; ++i)

          {

            if (samples[i].Flag)

            {

              s += samples[i].Weight;

            }

          }

          e.Value = s;

          e.Handled = true;

        }

      }

    }

  }

 

  internal class StyleConverter : IValueConverter

  {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

      bool _value = (bool)value;

      if (!_value)

      {

        return Color.Silver;

      }

      return Color.Black;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

      return value;

    }

  }



PK Pradeep Kumar Balakrishnan Syncfusion Team March 16, 2019 12:29 PM UTC

Hi Akihiko, 
 
Sorry for the delay. 
 
We are still validating the reported issue " Setting style in code behind is not updating properly" in Xamarin forms. We will log defect report or share you the solution for this issue in one business days March 18,2019. We appreciate your patience until then. 
 
New Query  
 
Can you please elaborate what you are trying to convey us in the sentence “exclude it from the sum”? . we have understand that you want to update the UnBound row every time the colour is changed.  
 
Please correct us if we miss understood on this. to give correct solution for your requirement. 
 
Regards, 
Pradeep Kumar B 
 



AN Akihiko Nakama March 18, 2019 05:22 AM UTC

Hi Pradeep,

Thank you for the update.

Sorry for the poor explanation. After all I am not good at English.

If you look at the attached file, you will understand what I want to do. Thank you.

Regards,

Akihiko Nakama


Attachment: description_108b9d12.zip


PK Pradeep Kumar Balakrishnan Syncfusion Team March 18, 2019 06:30 PM UTC

Hi Akihiko, 
 
Thank you for the pictorial representation of your requirement. 
 
Actually you have queried the same requirement in both the update. To avoid miscommunication we have asked confirmation. 
 
Query 1: Setting style is not updating properly. 
 
We have logged defect report for this issue “Setting style in code behind is not updating properly"” we have logged a defect report for the same. It will be fixed and included in 2019 Volume 1 SP -1 release which is expected to be rolled out by end of April 2019. We appreciate your patience until then. 
 
You can also track the status of the repot in following feedback link. 
 
Query 2: Need to change UnBound Row Value when ever the Switch Column state is changed. 
 
To achieve this requirement you have to hook DataGrid ValueChanged event in GridLoaded event. And refresh the UnBound row when ever value is switch column value is changed. Please refer the following code snippet for reference 
 
sfData.UnboundRows.Add(new GridUnboundRow() { Position = UnboundRowsPosition.FixedTop }); 
sfData.GridLoaded += SfData_GridLoaded; 
sfData.QueryUnboundRow += SfData_QueryUnboundRow; ; 
 
 
 
private void SfData_GridLoaded(object sender, GridLoadedEventArgs e) 
{ 
    sfData.ValueChanged += SfData_ValueChanged; 
} 
 
 
//UnBoundRow event code snippet. 
private void SfData_QueryUnboundRow(object sender, GridUnboundRowEventArgs e) 
{ 
    if(e.UnboundAction == UnboundActions.QueryData) 
    { 
        if(e.RowColumnIndex.ColumnIndex == 3) 
        { 
            e.Value = samples.Where(x => x.Flag == true).Sum(x => x.Weight); 
            e.Handled = true; 
        } 
    } 
} 
 
private void SfData_ValueChanged(object sender, Syncfusion.SfDataGrid.XForms.ValueChangedEventArgs e) 
{ 
    if (e.NewValue != e.CellValue && e.RowColumnIndex.ColumnIndex == 0) 
    { 
        this.sfData.InvalidateUnboundRow(this.sfData.UnboundRows[0]); 
    } 
} 
 
 
 
We have attached the sample for your reference in following link. 
 
 
Regards, 
Pradeep kumar B 



AN Akihiko Nakama March 19, 2019 09:03 AM UTC

Hi Pradeep,

Sorry to confuse you.

For “Query 1: Setting style is not updating properly.” I will wait until April to be corrected.

Thank you for sending me the code snippets for “Query 2: Need to change Unbound Row Value when the Switch Column state is changed.”. I were able to implement the desired processing in the program.

I look forward to working with you.

Regards,

Akihiko Nakama



SP Subburaj Pandian Veluchamy Syncfusion Team March 20, 2019 12:06 PM UTC

Hi Akihiko, 
 
Thank you for the update. 
 
Query 1: “Setting style is not updated properly”  
As mentioned, we will fix and include the issue fix in our 2019 volume 1 service pack release. We will let you know, once it has been rolled out.  
  
Query 2: We are happy that the given solution meets your requirement. Please let us know, if you need any further assistance. 
  
Regards,
Subburaj Pandian V  
 



JP Jagadeesan Pichaimuthu Syncfusion Team April 10, 2019 09:05 AM UTC

Hi Akihiko, 
 
We are glad to announce that our latest weekly NuGet package update version 17.1.0.40 has been rolled out with the mentioned issue fix and is available for download (nuget.org). Also we will include this fix in our upcoming 2019 volume 1 SP1 release which is expected to be available by mid of May, 2019. 
  
We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you would require any further assistance.  
 
Regards, 
Jagadeesan 


Loader.
Live Chat Icon For mobile
Up arrow icon