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;
}
}
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
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;
}
}
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
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]);
}
}
|
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