Hello all.
I'm looking for an example of runtime column creation in xamdatagrid with mvvm pattern.
I have already found some examples such as the following:
https://es.infragistics.com/community/blogs/b/blagunas/posts/xamdatagrid-dynamically-create-and-data-bind-columns-with-an-editor-of-your-choice
I have a trouble with this one because this define only one kind of editor.
In my scenario, a column could have one of the following type:
- Decimal,
- String,
- String with a ienumerable<string> which represent the allowed values
Each of these "type" have to be edited with a different editor.
Decimal with a numericeditor, string with a texteditor and the last one with a xamcomboeditor.
For holding these different kind of values, I created diferent classes.
Interface IValueHolder
{
string Name {get; set;}
object Value {get; set;}
}
public abstract class AbstractValueHolder<T> : IValueHolder
Private T _value;
Public AbstractValueHolder()
Object IValueHolder.Value{set{.......}get{.....}}
public T Value{get{ return _value;}set{_value=value;}
Class TableValueHolder: AbstractValueHolder<string>
Public object Value {get; set;}
Public IEnumerable<string> AllowedValues{get; set;}
In my scenario, a valueHolder can be accessed by its name
Public class ItemViewModel
Public IValueHolder this[](string name)
Return ValueHolderDictionay[name];}
......
My trouble is how to databind the ItemSource of a XamComboEditor in the xaml?
I tryed something like that, I don't have the code source in front of me now, ItemSource ="{Binding DataItem[Name].AllowedValue}"
but there is no ValueHolder with the name "Name".
I tryed too, to define it in the code behind, but it does not work as expected.
I hope that my request is clear, and that someone could answer!
Hello,
XamDataGrid supports datasources implementing the IDataErrorInfo interface, which will highlight the cell in red(default behavior) and display error message when validation doesn't pass. You can check our samples browser XamDataGrid->Display->IDataErrorInfoSupport for a sample how this works.
In case you don't want unsupported data to be entered, you can also hook to EditModeEnding event, check if validation is failing, and cancel the event. In case you don't really want to implement IDataErrorInfo, you can just use this method and throw a messagebox with an error text.
Sincerely,Tihomir TonevAssociate Software DeveloperInfragistics
Hello tihomir.
In My setter I throw exception if the value to set is incorrect.
By example, if the IValueHolder is a StringValueHolder in the setter I throw an exception if the length is greater than the Max length for this valueHolder.
But I would like to enforce the user to enter a valid value, then I would like to display the exception message if one is thrown in the setter.
Thanks a lot.
Where is the exception coming from? Do you mean ways to handle input validation errors via IDataErrorInfo, or you want to track errors during development?
Looking forward to your reply.
I solved this part with the DependencyProperty.
With this "link" between the fields of the DefaultFieldLayout of the xamdatagrid and the observableCollection<Field> in my ViewModel, I can in the ContextMenuOpening event (which is bind to a viewModel command) add only contextMenuItem for visible fields.
Can I ask you an another question?
What's the best practice for display messages when errors occurs on property set?
Now, exceptions are thrown but nothing is displayed.
Visibility is not a dependency property of the field, meaning you cannot bind to it, nor it will raise a trigger. I maybe don't understand your idea of tracking visibility, so could you please let me know how you intend to use the sample to track visibility of the fields.
On the other hand, an event that will rise when a field visibility is changed is FieldPositionChanged.
You can use this event and check the visibility of the fields in the current layout. All fields that are hidden will have their visibility set to collapsed.
private void XamDataGrid_FieldPositionChanged(object sender, Infragistics.Windows.DataPresenter.Events.FieldPositionChangedEventArgs e) { var grid = sender as XamDataGrid; var fieldlayout = grid.DefaultFieldLayout; foreach(var field in fieldlayout.Fields) { if(field.Visibility == Visibility.Collapsed) { //this field is hidden } } }
Sincerely,
Tihomir TonevAssociate Software DeveloperInfragistics