private Rule GetSelectedRule() { try { DataGridCellInfo cellInfo = RulesDataGrid.SelectedCells[0]; if (cellInfo == null) { return(null); } DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn; if (column == null) { return(null); } FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); var name = element.Tag.ToString(); foreach (Rule r in ExistingRules) { if (r.Name == name) { return(r); } } return(null); } catch (Exception ex) { LogHelper.LogError(ex); return(null); } }
public object GetCellValue(DataGridCellInfo dataGridCellInfo) { object r = null; if (dataGridCellInfo != null) { DataGridBoundColumn column = dataGridCellInfo.Column as DataGridBoundColumn; if (column != null) { FrameworkElement element = new FrameworkElement() { DataContext = dataGridCellInfo.Item }; BindingOperations.SetBinding ( target: element, dp: FrameworkElement.TagProperty, binding: column.Binding ); r = element.Tag; } } return(r); }
private string FindBoundProperty(DataGridColumn col) { DataGridBoundColumn boundColumn = col as DataGridBoundColumn; Binding binding = boundColumn.Binding as Binding; return(binding.Path.Path); }
void AddColumn(int type, string headerText, string binding) { if (type == 0 || type == 1) { DataGridBoundColumn col = null; if (type == 0) { col = new DataGridTextColumn(); } else { col = new DataGridCheckBoxColumn(); } col.Header = headerText; col.Binding = new Binding(binding); dataGrid.Columns.Add(col); } else if (type == 2) { DataGridComboBoxColumn col = new DataGridComboBoxColumn(); col.Header = headerText; //col.SelectedValueBinding = new Binding(binding); //col.SelectedValuePath = "Value"; //col.DisplayMemberPath = "Value"; col.TextBinding = new Binding(binding); col.ItemsSource = Enum.GetNames(typeof(ocNet.Lib.DBTable.TableFieldType)); dataGrid.Columns.Add(col); } }
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e) { DataGridBoundColumn column = e.Column as DataGridBoundColumn; Binding binding = column.Binding as Binding; switch (column.Header) { case "FullPath": column.MinWidth = 50; column.Header = columnNameFullPath; binding.Converter = new MediaFileNameConverter(); break; case columnNameDuration: column.MinWidth = 50; binding.Converter = new TimeSpanConverter(); break; case "Highlights": column.MinWidth = 50; column.Header = columnNameCount; binding.Converter = new HighlightsCountConverter(); binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; break; default: e.Cancel = true; break; } base.OnAutoGeneratingColumn(e); }
private string GetSelectedValue(DataGrid grid, int col) { DataGridCellInfo cellInfo1 = grid.SelectedCells[col]; if (cellInfo1 == null) { return(null); } DataGridBoundColumn column = cellInfo1.Column as DataGridBoundColumn; if (column == null) { return(null); } FrameworkElement element = new FrameworkElement() { DataContext = cellInfo1.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); return(element.Tag.ToString()); }
public string GetSelectedCellValue() { try { DataGridCellInfo cellInfo = dataGrid.SelectedCells[0]; if (cellInfo == null) { return(null); } DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn; if (column == null) { return(null); } FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); return(element.Tag.ToString()); } catch { MessageBox.Show("Please select a valid field."); return(null); } }
private static object?GetCellItem(this DataGridColumn column, object?item) { if (column is null || item is null) { return(null); } var descriptor = (PropertyDescriptor)column.GetValue(PropertyDescriptorProperty); if (descriptor != null) { return(descriptor.GetValue(item)); } var binding = column switch { DataGridBoundColumn c => c.Binding as Binding, CellTemplateColumn c => c.Binding as Binding, _ => null, }; if (binding is null) { return(null); } descriptor = TypeDescriptor.GetProperties(item) .OfType <PropertyDescriptor>() .SingleOrDefault(x => x.Name == binding.Path.Path); column.SetCurrentValue(PropertyDescriptorProperty, descriptor); return(descriptor?.GetValue(item)); }
static public void WPFDataGrid_CopyCell_Click(object sender, System.Windows.RoutedEventArgs e) { DependencyObject dep = _lastCell; if (_lastCell == null) { return; } // navigate further up the tree while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; // find the column that this cell belongs to DataGridBoundColumn col = _lastCell.Column as DataGridBoundColumn; // find the propertyName that this column is bound to Binding binding = col.Binding as Binding; string boundPropertyName = binding.Path.Path; // find the object that is related to this row object data = row.Item; // extract the propertyName value PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data); PropertyDescriptor property = properties[boundPropertyName]; object value = property.GetValue(data).ToString(); Clipboard.SetText(value.ToString()); }
public static string ColumnToStringTable(this DataGrid dataGrid, DataGridBoundColumn column) { if (dataGrid == null || column == null) { return(null); } var sb = new StringBuilder(); foreach (var item in dataGrid.Items) { Binding binding = (Binding)column.Binding; string propertyName = binding.Path; Type type = item.GetType(); PropertyInfo propertyInfo = type.GetProperty(propertyName); if (propertyInfo != null) { object obj = propertyInfo.GetValue(item); string value = obj.Formatted(MaxValueLength); sb.AppendLine(value); } else { sb.AppendLine('(' + propertyName + ')'); } //object content = column.GetCellValue(item, column.ClipboardContentBinding); } return(sb.ToString()); }
public static string GetSortMemberPath(DataGridColumn column) { string sortPropertyName = column.SortMemberPath; if (string.IsNullOrEmpty(sortPropertyName)) { DataGridBoundColumn boundColumn = column as DataGridBoundColumn; if (boundColumn != null) { Binding binding = boundColumn.Binding as Binding; if (binding != null) { if (!string.IsNullOrEmpty(binding.XPath)) { sortPropertyName = binding.XPath; } else if (binding.Path != null) { sortPropertyName = binding.Path.Path; } } } } return(sortPropertyName); }
//private static int FindColumnIndex (DataGridColumn col) //{ // DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer (col) as DataGrid; // int index = dataGrid.ItemContainerGenerator.IndexFromContainer (col); // return index; //} private static object ExtractBoundValue(DataGridRow row, DataGridCell cell, out int column) { column = -1; // find the column that this cell belongs to DataGridBoundColumn col = cell.Column as DataGridBoundColumn; if (col == null) { return(null); } column = cell.Column.DisplayIndex; // find the property that this column is bound to Binding binding = col.Binding as Binding; string boundPropertyName = binding.Path.Path; // find the object that is related to this row object data = row.Item; if (data == null) { return(null); } // extract the property value PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data); if (properties == null) { return(null); } PropertyDescriptor property = properties[boundPropertyName]; object value = property.GetValue(data); return(value); }
public string GetSelectedCellValue() { DataGridCellInfo cellInfo = new DataGridCellInfo(); try { cellInfo = dgTabla.SelectedCells[0]; } catch (Exception) { } if (cellInfo == null) { return(null); } DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn; if (column == null) { return(null); } FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); return(element.Tag.ToString()); }
// Retrieve a cell's data-bound value public static object GetBoundValue(this DataGridCell cell) { // Get the cell's column DataGridBoundColumn col = cell.Column as DataGridBoundColumn; // Get the name of the property bound to the column Binding binding = col.Binding as Binding; string boundPropertyName = binding.Path.Path; // Get the cell's row var parent = VisualTreeHelper.GetParent(cell); while (parent != null && parent.GetType() != typeof(DataGridRow)) { parent = VisualTreeHelper.GetParent(parent); } DataGridRow row = parent as DataGridRow; // Get the object in the DataGridRow object data = row.Item; // Extract the property's value PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data); PropertyDescriptor property = properties[boundPropertyName]; object value = property.GetValue(data); return(value); }
private static IList <ColumnDescriptor> ColumnsSourceChanged(IAvaloniaObject o, IList <ColumnDescriptor> arg2) { if (o is DataGrid dataGrid) { dataGrid.Columns.Clear(); foreach (var col in arg2) { DataGridBoundColumn column = col.CheckboxMember ? new DataGridCheckBoxColumn() : new DataGridTextColumn(); column.Header = new DataGridColumnHeader() { Content = col.HeaderText }; column.IsReadOnly = !col.CheckboxMember; column.Binding = new Binding(col.DisplayMember); dataGrid.Columns.Add(column); } } else if (o is GridView gridView) { var columns = arg2.Select(col => new GridColumnDefinition() { Name = col.HeaderText, Property = col.DisplayMember, PreferedWidth = (int)(col.PreferredWidth ?? 100), Checkable = col.CheckboxMember }).ToList(); gridView.Columns = columns; } return(arg2); }
public string GetSelectedCellValue() { DataGridCellInfo cellInfo = AssetListSelect.SelectedCells[0]; if (cellInfo == null) { return(null); } DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn; if (column == null) { return(null); } FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); return(element.Tag.ToString()); }
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { string s = (string)e.Column.Header; string changed_header; int index = s.IndexOf('_'); if (s.Length > index + 1) { changed_header = s.Replace(s, s.Remove(0, index + 1)); e.Column.Header = changed_header; } //s = (string)e.Column.Header; //if (s.Contains("/")) //{ // index = s.IndexOf('/'); // changed_header = s.Replace('/', '-'); // e.Column.Header = changed_header; //} if (e.Column is DataGridBoundColumn) { DataGridBoundColumn dataGridBoundColumn = e.Column as DataGridBoundColumn; dataGridBoundColumn.Binding = new Binding("[" + e.PropertyName + "]"); //dataGridBoundColumn.Binding = new Binding(string.Format("[{0}]", e.PropertyName)); } }
private void dataGridInfo_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { var desc = e.PropertyDescriptor as PropertyDescriptor; var att = desc.Attributes[typeof(ColumnNameAttribute)] as ColumnNameAttribute; if (att != null) { DataGridBoundColumn column = e.Column as DataGridBoundColumn; if (column != null) { column.Binding = new Binding(e.PropertyName); Style elementStyle = new Style(typeof(TextBlock)); elementStyle.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.WrapWithOverflow)); column.ElementStyle = elementStyle; } e.Column.Header = att.Name; if (att.Name != "کد درس") { e.Column.IsReadOnly = true; } else { e.Column.IsReadOnly = false; } } }
private void ValidateColumnVisibility() { if (Items.Count == 0 || Items[0] == CollectionView.NewItemPlaceholder) { return; } var itemClass = Items[0].GetType(); foreach (var column in Columns) { if (column.GetType() == typeof(DataGridTextColumn)) { // find the property that this column is bound to DataGridBoundColumn boundColumn = column as DataGridBoundColumn; Binding binding = boundColumn.Binding as Binding; string boundPropertyName = binding.Path.Path; var prop = itemClass.GetProperty(boundPropertyName); // Validating Column.Visibility when first value is null // when all values are null -> Visibility.Collapsed // bound value not a string -> Visibility.Visible // all bound strings empty -> Visibility.Collapsed if (prop.GetValue(Items[0]) == null) { column.Visibility = Visibility.Collapsed; foreach (var item in Items) { if (item != CollectionView.NewItemPlaceholder) { if (prop.GetValue(item) != null) { if (prop.GetValue(item).GetType() != typeof(String)) { column.Visibility = Visibility.Visible; } else if (String.IsNullOrEmpty(prop.GetValue(item) as String) == false) { column.Visibility = Visibility.Visible; } } } } } // First value not null and all bound strings empty -> Visibility.Collapsed else if (prop.GetValue(Items[0]).GetType() == typeof(String)) { column.Visibility = Visibility.Collapsed; foreach (var item in Items) { if (item != CollectionView.NewItemPlaceholder) { if (String.IsNullOrEmpty(prop.GetValue(item) as String) == false) { column.Visibility = Visibility.Visible; } } } } } } }
private object GetCellValue(DataGridBoundColumn column) { var element = new FrameworkElement() { DataContext = CurrentCell.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); return(element.Tag); }
private string FindBoundProperty(DataGridColumn col) { DataGridBoundColumn boundColumn = col as DataGridBoundColumn; // find the property that this column is bound to Binding binding = boundColumn.Binding as Binding; string boundPropertyName = binding.Path.Path; return(boundPropertyName); }
private string getValuePropertyBindingPath(DataGridColumn column) { string path = String.Empty; if (column is DataGridBoundColumn) { DataGridBoundColumn bc = column as DataGridBoundColumn; path = (bc.Binding as Binding).Path.Path; } else if (column is DataGridTemplateColumn) { DataGridTemplateColumn tc = column as DataGridTemplateColumn; object templateContent = tc.CellTemplate.LoadContent(); if (templateContent != null && templateContent is TextBlock) { TextBlock block = templateContent as TextBlock; BindingExpression binding = block.GetBindingExpression(TextBlock.TextProperty); path = binding.ParentBinding.Path.Path; } } else if (column is DataGridComboBoxColumn) { DataGridComboBoxColumn comboColumn = column as DataGridComboBoxColumn; path = null; Binding binding = comboColumn.SelectedValueBinding as Binding ?? (comboColumn.SelectedItemBinding) as Binding ?? comboColumn.SelectedValueBinding as Binding; if (binding != null) { path = binding.Path.Path; } if (comboColumn.SelectedItemBinding != null && comboColumn.SelectedValueBinding == null) { if (path?.Trim().Length > 0) { if (DataGridComboBoxExtensions.GetIsTextFilter(comboColumn)) { path += "." + comboColumn.DisplayMemberPath; } else { path += "." + comboColumn.SelectedValuePath; } } } } return(path); }
private void AddColumnToDataGrid(DataGrid dataGrid, DataGridBoundColumn column, string header, int width, string binding) { DataGridBoundColumn temp = column; temp.Header = header; temp.Width = width; temp.IsReadOnly = true; temp.Binding = new Binding(binding); temp.IsReadOnly = false; dataGrid.Columns.Add(temp); }
private void usersDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { DataGridBoundColumn column = e.Column as DataGridBoundColumn; if (column != null) { Style elementStyle = new Style(typeof(TextBlock)); elementStyle.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.WrapWithOverflow)); column.ElementStyle = elementStyle; } }
/************************************************************************/ /// <summary> /// Formats the column to display a date and makes the column fixed width. /// </summary> /// <param name="col">The column.</param> /// <param name="dateFormat">The desired date format. Null (the default) uses the application default format</param> /// <param name="width">The desired width. Default is <see cref="DefaultColumnWidth"/>.</param> /// <param name="toLocal">If true (the default), adds a converter to display the bound date as local date/time</param> /// <returns>The column</returns> public static DataGridBoundColumn MakeDate(this DataGridBoundColumn col, string dateFormat = null, int width = DefaultColumnWidth, bool toLocal = true) { if (toLocal) { ((DataBinding)col.Binding).Converter = new DateUtcToLocalConverter(); } col.Binding.StringFormat = !string.IsNullOrEmpty(dateFormat) ? dateFormat : Default.Format.DataGridDate; col.MakeFixedWidth(width); return(col); }
public void Execute(BinderContext context) { DataGridBoundColumn boundColumn = context.TargetObject as DataGridBoundColumn; if (boundColumn != null) { //context.PrepareBinding(); boundColumn.Binding = context.Binding; context.Complete = true; } }
public static void SetElementStyle([NotNull] this DataGridBoundColumn column, [CanBeNull] Binding languageBinding, [CanBeNull] Binding flowDirectionBinding) { var elementStyle = new Style(typeof(TextBlock), column.ElementStyle); var setters = elementStyle.Setters; setters.Add(new Setter(FrameworkElement.LanguageProperty, languageBinding)); setters.Add(new Setter(FrameworkElement.FlowDirectionProperty, flowDirectionBinding)); elementStyle.Seal(); column.ElementStyle = elementStyle; }
public string GetSelectedCellValue(int index) { DataGridCellInfo cellInfo = DataGridEmployee.SelectedCells[index]; if (cellInfo == null) return null; DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn; if (column == null) return null; FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item }; BindingOperations.SetBinding(element, TagProperty, column.Binding); return element.Tag.ToString(); }
private object ExtractBoundValue(DataGridRow row, DataGridCell cell) { DataGridBoundColumn col = cell.Column as DataGridBoundColumn; Binding binding = col.Binding as Binding; string boundPropertyName = "Username"; object data = row.Item; PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data); PropertyDescriptor property = properties[boundPropertyName]; return(property.GetValue(data)); }
public static void SetElementStyle(this DataGridBoundColumn column, Binding languageBinding, Binding flowDirectionBinding) { Contract.Requires(column != null); var elementStyle = new Style(typeof(TextBlock), column.ElementStyle); var setters = elementStyle.Setters; setters.Add(new Setter(FrameworkElement.LanguageProperty, languageBinding)); setters.Add(new Setter(FrameworkElement.FlowDirectionProperty, flowDirectionBinding)); elementStyle.Seal(); column.ElementStyle = elementStyle; }