I am using XamCurrencyEditor as the cell editor in a template column which uses a data type of "decimal?". When the value in the cell is blanked out, the cell reverts back to the original value. I suspect the XamCurrencyEditor validation fails when the data entered is blank and instead of setting the data value behind the cell to null, it reverts back to the previous value.
How can I configure XamCurrencyEditor to allow blank input and set the underlying data value to null?
My XAML looks like:
<ig:XamGrid.Resources> <Style TargetType="{x:Type igEditors:XamCurrencyEditor}"> <EventSetter Event="GotFocus" Handler="XamCurrencyEditorGotFocus"/> <Setter Property="Mask" Value="{}{currency:-4.2}"/> <Setter Property="BorderThickness" Value="1,1,1,1" /> </Style>
Hello Gary,
As long as the property that is bound to the XamCurrencyEditor is a nullable type it should accept an empty value. I just tested this and it behaved this way. You can find my sample attached.
Can you modify my sample to reproduce your issue so I can see what is happening?
Do you still require assistance on this matter? If so can you modify my sample to reproduce your issue and send it back to me?
So I create a custom cell display/editor entries and use them in a template column like this:
// Pricing column display = new FrameworkElementFactory(typeof(TextBlock)); binding = new Binding("Price[" + i + "]"); binding.StringFormat = "{0:C}"; display.SetBinding(TextBlock.TextProperty, binding); dollarCellDisplay = new DataTemplate() { VisualTree = display }; dollarCellDisplay.Seal(); editor = new FrameworkElementFactory(typeof(XamCurrencyEditor)); binding = new Binding("Price[" + i + "]"); editor.SetBinding(XamCurrencyEditor.TextProperty, binding); dollarCellEditor = new DataTemplate() { VisualTree = editor }; dollarCellEditor.Seal(); TemplateColumn priceColumn = new TemplateColumn() { Key = "Price[" + i + "]", HeaderText = "S" + i, HorizontalContentAlignment = HorizontalAlignment.Right, VerticalContentAlignment = VerticalAlignment.Center, IsReadOnly = false, ItemTemplate = dollarCellDisplay, EditorTemplate = dollarCellEditor, Width = new ColumnWidth(80.0D, false) };
The grid's item source contains objects like:
public class RowData { public decimal?[] Price { get; set; } }
The template columns are created in a loop so you have the same number of columns as the size of the RowData.Price member. So you have a list of RowData objects that each represent a row and each RowData's Price array member is the same length, so that length is the number of columns. The display/editor template binds to the Price[] members. The type for these is decimal?, so the XamCurrencyEditor should be referencing a nullable type, and in fact the RowData objects are initialized with null values and they display blank in the grid (due to the dollarCellDisplay object), but once the XamCurrencyEditor is activated, it requires a value to be entered.
Any ideas on how to determine the type the XamCurrencyEditor actually sees? It seems it should be looking at a decimal?, but the behavior shows otherwise.
Do you mean how the cell get's a red border if you remove the value? If I use your code to generate the columns, I can get red borders to appear around the cell but this doesn't stop me from removing the value and submitting a null value. The red border is occurring because of first chance exceptions that are occurring. You can see them in the Visual Studio output window, and these are all formatting exceptions. The code provided is trying to bind a string property (XamCurrencyEditor.Text) to a decimal so the exception occurs. The code should be using the Value property instead.
I have attached an updated version of my last sample using the code you provided.
Hi Rob, yes, the red border is one of the things I see, and you are right, my issue is due to me binding to the TextProperty instead of the ValueProperty. I changed the binding for the editors and I can now blank the $ value.
Thanks!
Hi Gary,
Awesome! I'm glad it was a simple fix.