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