public void PropertyBindingWithLambdaExpressionTest() { TestClass o = new TestClass(); PropertyBinding binding = new PropertyBinding(o, () => o.IntProperty); o.IntProperty = 1907; Assert.AreEqual(o.IntProperty, binding.GetValue()); binding.SetValue(2012); Assert.AreEqual(o.IntProperty, binding.GetValue()); }
public void PropertyBindingWithStringTest() { TestClass o = new TestClass(); PropertyBinding binding = new PropertyBinding(o, "IntProperty"); o.IntProperty = 1907; Assert.AreEqual(o.IntProperty, binding.GetValue()); binding.SetValue(2012); Assert.AreEqual(o.IntProperty, binding.GetValue()); }
public override object GetValue() { if (_property == null) { return(null); } object instance = _target.GetValue(); if (NullHelper.IsNull(instance)) { return(null); } object result; try { result = _property.GetValue(instance); } catch (NQueryException) { throw; } catch (Exception ex) { throw ExceptionBuilder.PropertyBindingGetValueFailed(ex); } return(NullHelper.UnifyNullRepresentation(result)); }
/// <summary> /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="x">The first object to compare.</param> /// <param name="y">The second object to compare.</param> /// <returns> /// Value /// Condition /// Less than zero /// <paramref name="x" /> is less than <paramref name="y" />. /// Zero /// <paramref name="x" /> equals <paramref name="y" />. /// Greater than zero /// <paramref name="x" /> is greater than <paramref name="y" />. /// </returns> /// <exception cref="T:System.ArgumentException"> /// Neither <paramref name="x" /> nor <paramref name="y" /> implements the <see cref="T:System.IComparable" /> /// interface. /// -or- /// <paramref name="x" /> and <paramref name="y" /> are of different types and neither one can handle comparisons with /// the other. /// </exception> protected override int Compare(string propertyName, object x, object y) { object xo = PropertyBinding.GetValue(x, propertyName); object yo = PropertyBinding.GetValue(y, propertyName); if (xo == null && yo == null) { return(0); } if (xo == null) { return(-1); } if (yo == null) { return(1); } return(string.CompareOrdinal(xo.ToString().ToLowerInvariant(), yo.ToString().ToLowerInvariant())); }
/// <summary> /// Creates a <see cref="DataTable" /> containing the data within the specified <see cref="ListView" />. /// </summary> /// <param name="listView">The list view.</param> /// <param name="eventHandler">The event handler.</param> /// <returns> /// A <see cref="DataTable" /> of the contents of the list view. /// </returns> public static DataTable AsDataTable(this ListView listView, ProgressChangedEventHandler eventHandler) { DataTable table = new DataTable(); table.Locale = CultureInfo.InvariantCulture; if (listView == null) { return(null); } GridView gridView = listView.View as GridView; if (gridView == null) { return(null); } // Create the binding dictionary. Dictionary <DataColumn, string> bindings = new Dictionary <DataColumn, string>(); foreach (var column in gridView.Columns) { string propertyName; string columnName; // Determine the property for the column binding. Binding binding = column.DisplayMemberBinding as Binding; if (binding == null || binding.Path == null) { propertyName = GetPropertyName(column); } else { propertyName = binding.Path.Path; } if (string.IsNullOrEmpty(propertyName)) { continue; } columnName = column.Header == null ? propertyName : string.Format(CultureInfo.InvariantCulture, "{0}", column.Header); // Create the new binding. DataColumn header = table.Columns.Add(columnName); bindings.Add(header, propertyName); } int count = 0; // Iterate through all of the items. foreach (var item in listView.Items) { // Increment the counter. count++; // Create the new row and update the values. DataRow row = table.NewRow(); // Use reflection to determine which columns get which values. foreach (KeyValuePair <DataColumn, string> entry in bindings) { object value = PropertyBinding.GetValue(item, entry.Value); row[entry.Key] = value; } // Add the new row. table.Rows.Add(row); if (eventHandler != null) { int progressPercentage = (int)(count / (float)listView.Items.Count * 100); eventHandler(listView, new ProgressChangedEventArgs(progressPercentage, row)); } } return(table); }