data-binding
Binding JSON to nested Grails Domain Objects
Since this question got upvoted several times I would like to share what I did in the end: Since I had some more requirements to be implemented like security etc. I implemented a service layer which hides the domain objects from the controllers. I introduced a “dynamic DTO layer” which translates Domain Objects to Groovy … Read more
Binding a WPF ShortCut Key to a Command in the ViewModel
The following code can be used to bind a shortcut key directly to a command: <Window.InputBindings> <KeyBinding Command=”{Binding Path=NameOfYourCommand}” Key=”O” Modifiers=”Control”/> </Window.InputBindings> Add this after Window.Resources in the XAML code of your view.
WPF: Binding a ContextMenu to an MVVM Command
The problem is that the ContextMenu it not in the visual tree, so you basically have to tell the Context menu about which data context to use. Check out this blogpost with a very nice solution of Thomas Levesque. He creates a class Proxy that inherits Freezable and declares a Data dependency property. public class … Read more
Best practice when not implementing IValueConvert.ConvertBack
The documentation for IValueConverter.ConvertBack recommends returning DependencyProperty.UnsetValue. The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the ConvertBack method, or any uncaught exceptions that are thrown by methods that the ConvertBack method calls, are treated as run-time errors. Handle anticipated problems by returning … Read more
How do I databind a ColumnDefinition’s Width or RowDefinition’s Height?
Create a IValueConverter as follows: public class GridLengthConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double val = (double)value; GridLength gridLength = new GridLength(val); return gridLength; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { GridLength val = (GridLength)value; return val.Value; } } You can … Read more
PropertyChanged for indexer property
According to this blog entry, you have to use “Item[]”. Item being the name of the property generated by the compiler when using an indexer. If you want to be explicit, you can decorate the indexer property with an IndexerName attribute. That would make the code look like: public class IndexerProvider : INotifyPropertyChanged { [IndexerName … Read more
WPF Binding FallbackValue set to Binding
What you are looking for is something called PriorityBinding (#6 on this list) (from the article) The point to PriorityBinding is to name multiple data bindings in order of most desirable to least desirable. This way if the first binding fails, is empty and/or default, another binding can take it’s place. e.g. <TextBox> <TextBox.Text> <PriorityBinding> … Read more
How to perform LINQ query over Enum?
return Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>().Where((n, x) => x < 4); If you want to be able to change the list of items, just add them into a List<Activity.StatusEnum> and use Contains: var listValid = new List<Activity.StatusEnum>() { Activity.StatusEnum.Open, Activity.StatusEnum.Rejected, Activity.StatusEnum.Accepted, Activity.StatusEnum.Started }; return Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>().Where(n => listValid.Contains(n));
How to hide a TemplateField column in a GridView
protected void OnRowCreated(object sender, GridViewRowEventArgs e) { e.Row.Cells[columnIndex].Visible = false; } If you don’t prefer hard-coded index, the only workaround I can suggest is to provide a HeaderText for the GridViewColumn and then find the column using that HeaderText. protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e) { ((DataControlField)UsersGrid.Columns .Cast<DataControlField>() .Where(fld => fld.HeaderText == “Email”) .SingleOrDefault()).Visible = … Read more