Hi all,
I have a dataset-table with column BackColor of type integer and have bound the colorpicker ColorWin32-property to the datasource. It seems that the alpha value gets lost when saving to the database. Choosing color transparent ist restored to color white.
I have tried to bind the colorpicker Color-property instead and to use a datafilter to convert the values. But I get an internal editor converting error:
InnerException Message: Invalid conversion. bei Infragistics.Win.UltraWinEditors.UltraColorPicker.OnValueChanged(EventArgs args) bei Infragistics.Win.UltraWinEditors.TextEditorControlBase.OnValueChanged(Object sender, EventArgs args) bei Infragistics.Win.EmbeddableEditorBase.RaiseValueChangedEvent()
My datafilter implements to conversions:
case Infragistics.Win.ConversionDirection.EditorToOwner: if ( args.Value == DBNull.Value || args.Value == null || args.Value.Equals( System.DBNull.Value ) ) return args.Value;
args.Handled = true;
Color color = (Color)args.Value;
// return argb value to tha dataset and to store in db
return color.ToArgb(); case Infragistics.Win.ConversionDirection.OwnerToEditor: if ( args.Value == DBNull.Value || args.Value == null || args.Value.Equals( System.DBNull.Value ) ) return args.Value; args.Handled = true;
// return color class to the bound color-property return (Color)args.Value;
Does anybody can point me in the right direction to achieve this.
Regards
Markus
Hi Markus,
Sorry, I should have tested it with colors that included an alpha channel. I tested that the methods worked with an alpha channel and then when I wrote the sample, I forgot to include that.
I'm glad you got it working, though. :)
Hi Mike,
Thank you for the sample. I have tested your sample code in my project and I have 2 problems:
- I still loosing alpha value, I guess. It seems that colors 255,x,x,x converted to -1. Choosing Transparent, save, color White is selected in the drop down of color picker.
- the color is not a known color. Color values instead of names are displayed.
I have found ColorConverter-class and ColorPickerEditor.ColorFromString and I have modified your sample code. This code works nicely: Names are displaed and selecting Transparent remains transparent, White remains White, etc.
void binding_Format( object sender, ConvertEventArgs e ) { if ( null == e.Value || e.Value is DBNull ) { // Do nothing. return; } if ( e.Value is Color ) return; if ( e.Value is int ) { Color color = Color.FromArgb( ( (int)e.Value ) ); string colorString = new ColorConverter().ConvertToString( color ); Color namedColor = Infragistics.Win.ColorPickerEditor.ColorFromString( colorString, color, true ); e.Value = namedColor; return; } }
The ColorWin32, ColorHtml, and ColorOle properties are all handled by the DotNet framework's ColorTranslator class. I did a little research and it looks like none of these support an alpha channel in the color.
You can see this yourself, if you like:
Color color = Color.FromArgb(128, 128, 128, 128); int i = ColorTranslator.ToWin32(color); Color color2 = ColorTranslator.FromWin32(i);
If you run that code and examine color2, you can see the alpha is 255.
The good news is that the Color class itself has methods for converting a color to an int and vice versa while maintaining the alpha channel. So you can achieve what you want using Color.FromArgb and color.ToArgb.
So what you can do is use the Parse and Format events of the Binding to do the conversion.
I have attached a small sample project here demonstrating how it's done.