public MainWindow() { DataContext = this; InitializeComponent(); CalcObservable = DatabaseQueries.ShiftInputSourceObserv(SelectedEmployee.Key, DateFilter); MyDataGrid.SetBinding(ComboBox.ItemsSourceProperty, new Binding(nameof(CalcObservable)) { Source = this }); }
private void loadGridData() { // add columns ObservableCollection <DataGridColumn> columns = createColumns(); foreach (DataGridColumn col in columns) { MyDataGrid.Columns.Add(col); } // add rows cvSource = new CollectionViewSource(); cvSource.Source = createRows(); // set datagrid items Binding itemsSourceBinding = new Binding(); itemsSourceBinding.Source = cvSource; MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, itemsSourceBinding); }
public DocUIDataGrid(XmlNode xmlNode, XmlSchemaAnnotated xsdNode, Panel contentpanel, Panel overlaypanel, DynamicForm parentForm) : base(xmlNode, xsdNode, contentpanel, overlaypanel, parentForm) { XmlSchemaElement schemaEl = xsdNode as XmlSchemaElement; if (schemaEl != null) { _dgrid = new MyDataGrid() { Margin = new Thickness(5), CanUserReorderColumns = false }; _dgrid.CellEditEnding += (s, e) => { hasPendingChanges(); }; _dgrid.SelectionUnit = DataGridSelectionUnit.Cell; this.xmlNode = xmlNode; XmlSchemaSequence seq1 = XmlSchemaUtilities.tryGetSequence(schemaEl.ElementSchemaType); XmlDataProvider provider = new XmlDataProvider(); provider.Document = xmlNode.OwnerDocument; Binding myNewBindDef = new Binding(); myNewBindDef.Source = provider; myNewBindDef.XPath = "//" + xmlNode.Name + "/*"; _dgrid.SetBinding(DataGrid.ItemsSourceProperty, myNewBindDef); _dgrid.AutoGenerateColumns = false; if (seq1 != null && seq1.Items.Count > 0) { _el = seq1.Items[0] as XmlSchemaElement; XmlSchemaSequence seq2 = XmlSchemaUtilities.tryGetSequence((seq1.Items[0] as XmlSchemaElement).ElementSchemaType); if (seq2 != null) { foreach (XmlSchemaElement child in seq2.Items) { XmlSchemaType type = child.ElementSchemaType; string result1; GetColumn result2; DataGridColumn col; if (DynamicForm.Components.TryGetValue(type.QualifiedName.Name, out result1) && ColDict.TryGetValue(result1, out result2)) { var b = new Binding { XPath = child.Name }; col = result2(b); if (result1 == "combobox") { DataGridTemplateColumn col2 = col as DataGridTemplateColumn; FrameworkElementFactory fact = col2.CellTemplate.VisualTree; List<string> list = new List<string>(); IEnumerable<XmlSchemaEnumerationFacet> enumFacets = XmlSchemaUtilities.tryGetEnumRestrictions(child.ElementSchemaType); foreach (var facet in enumFacets) { list.Add(facet.Value); } fact.SetValue(ComboBox.ItemsSourceProperty, list); string widthstr = XmlSchemaUtilities.tryGetUnhandledAttribute(child, "width"); int width = 0; if (widthstr != "") width = Int32.Parse(widthstr); else width = 50; col2.Width = width; } } else { col = new DataGridTextColumn(); DataGridTextColumn textcol = col as DataGridTextColumn; var b = new Binding { XPath = child.Name }; textcol.Binding = b; textcol.Width = new DataGridLength(1, DataGridLengthUnitType.Star); } col.Header = child.Name; _dgrid.Columns.Add(col); } DataTemplate dt = new DataTemplate(); FrameworkElementFactory templatebutton = new FrameworkElementFactory(typeof(Button)); FrameworkElementFactory img = new FrameworkElementFactory(typeof(Image)); Binding bind = new Binding { XPath = ".", Mode = BindingMode.TwoWay }; BitmapImage bi3 = EmbeddedResourceTools.GetImage("Com.Xploreplus.DocUI.Resources.Images.component.delete.png"); img.SetValue(Image.SourceProperty, bi3); templatebutton.AddHandler(Button.ClickEvent, new RoutedEventHandler(click_Delete)); templatebutton.AppendChild(img); templatebutton.SetBinding(Button.TagProperty, bind); dt.VisualTree = templatebutton; DataGridTemplateColumn buttoncol = new DataGridTemplateColumn(); buttoncol.CanUserResize = false; buttoncol.CellTemplate = dt; _dgrid.Columns.Add(buttoncol); _dgrid.RowHeight = 25; _dgrid.CanUserResizeRows = false; _dgrid.GotFocus += gotFocus; _dgrid.PreparingCellForEdit += checkEmptyRow; _dgrid.PreparingCellForEdit += (s, e) => { this._isEditing = true; }; _dgrid.CellEditEnding += (s, e) => { this._isEditing = false; }; } } } }