private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { switch (e.PropertyName) { case "Bitrate": e.Column.Header = Properties.Resources.columnBitrate; e.Column.IsReadOnly = true; e.Column.CanUserSort = true; break; case "ContentType": e.Column.Header = "MIME(ContentType)"; e.Column.IsReadOnly = true; break; case "Url": var style = new Style(typeof(TextBlock)); style.Setters.Add(new EventSetter(Hyperlink.ClickEvent, (RoutedEventHandler)Hyperlink_Clickhandler)); var c = new DataGridHyperlinkColumn() { Header = Properties.Resources.columnUrl, ElementStyle = style, Binding = new Binding("Url"), CanUserSort = true, IsReadOnly = true }; e.Column = c; break; } }
private void AutoGenColumns(DataGridAutoGeneratingColumnEventArgs e) { try { var config = Sys.Columns.Attendance.Where(x => x.Key == e.PropertyName).First(); if (e.PropertyName == "Name") { var style = new Style(typeof(TextBlock)); style.Setters.Add(new EventSetter(Hyperlink.ClickEvent, (RoutedEventHandler)EventSetter_OnHandler)); var column = new DataGridHyperlinkColumn { Header = config.Value.description, Binding = new Binding("Name"), ElementStyle = style }; e.Column = column; return; } if (!config.Value.visiable) { e.Cancel = true; } } catch (Exception) { } }
public override void Perform() { PropertyInfo[] props = typeof(ConstrainedDataGridItem).GetProperties(); for (int i = 0; i < props.Length; i++) { DataGridColumn col = null; if (props[i].PropertyType == typeof(Boolean)) { col = new DataGridCheckBoxColumn(); } if (props[i].PropertyType == typeof(Enumerations)) { col = new DataGridComboBoxColumn(); Array array = Enum.GetNames(typeof(Enumerations)); ((DataGridComboBoxColumn)col).ItemsSource = array; } if (props[i].PropertyType == typeof(Uri)) { col = new DataGridHyperlinkColumn(); ((DataGridHyperlinkColumn)col).ContentBinding = new Binding(props[i].Name); } else { col = new DataGridTextColumn(); } DataGrid.Columns.Add(col); } Window.Content = DataGrid; DataGrid.Measure(Window.RenderSize); if (ItemList.Count != 0) { for (int i = 0; i < ItemsCount; i++) { AddItem(DataGrid, Add, i, ItemList[(int)(Rate * ItemList.Count)]); } } if (DataGrid.Items.Count != 0) { if (RemoveByIndex) { DataGrid.Items.RemoveAt((int)(Rate * DataGrid.Items.Count)); } else { DataGrid.Items.Remove(DataGrid.Items[(int)(Rate * DataGrid.Items.Count)]); } } DataGrid.UpdateLayout(); }
private void Statistic() { statisticDataGrid.Columns.Clear(); DataGridTemplateColumn conditioncolumn = new DataGridTemplateColumn(); //conditioncolumn.CellTemplate = GetColumnDataTemplate("Group"); conditioncolumn.CellTemplate = GetColumnCellTemplate("Group"); this.statisticDataGrid.Columns.Add(conditioncolumn); Style styleRight = new Style(typeof(TextBlock)); Setter setRight = new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center); //右对齐 Setter setVertical = new Setter(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); //垂直居中 styleRight.Setters.Add(setRight); styleRight.Setters.Add(setVertical); foreach (NameValuePair <string> group in statisticGroup) { DataGridHyperlinkColumn linkcolumn = new DataGridHyperlinkColumn() { Header = group.Value, MinWidth = 50 }; linkcolumn.Binding = new Binding { Path = new PropertyPath("[" + group.Name + "]") }; linkcolumn.TargetName = group.Name; linkcolumn.ElementStyle = styleRight; //DataGridTextColumn columnname = new DataGridTextColumn { Header = group, MinWidth = 100 }; //columnname.Binding = new Binding { Path = new PropertyPath("[" + group + "]") }; //this.statisticDataGrid.Columns.Add(columnname); //DataGridTemplateColumn cellcolumn = new DataGridTemplateColumn(); //cellcolumn.Header = group; //cellcolumn.CellTemplate = GetColumnCellTemplate(group); this.statisticDataGrid.Columns.Add(linkcolumn); } DataGridTemplateColumn btncolumn = new DataGridTemplateColumn(); btncolumn.Header = "详情"; btncolumn.CellTemplate = GetColumnCellButtonTemplate("Group"); this.statisticDataGrid.Columns.Add(btncolumn); }
protected void SetupDataGridItemsSource() { DataGrid.Items.Clear(); ObservableCollection <ConstrainedDataGridItem> observableCollection = new ObservableCollection <ConstrainedDataGridItem>(ItemsSource); DataGrid.ItemsSource = observableCollection; DataGrid.Measure(Window.RenderSize); if (DataGrid.AutoGenerateColumns == false) { PropertyInfo[] props = typeof(ConstrainedDataGridItem).GetProperties(); for (int i = 0; i < props.Length; i++) { DataGridColumn col = null; if (props[i].PropertyType == typeof(Boolean)) { col = new DataGridCheckBoxColumn(); } if (props[i].PropertyType == typeof(Enumerations)) { col = new DataGridComboBoxColumn(); Array array = Enum.GetNames(typeof(Enumerations)); ((DataGridComboBoxColumn)col).ItemsSource = array; } if (props[i].PropertyType == typeof(Uri)) { col = new DataGridHyperlinkColumn(); ((DataGridHyperlinkColumn)col).ContentBinding = new Binding(props[i].Name); } else { col = new DataGridTextColumn(); } DataGrid.Columns.Add(col); } DataGrid.UpdateLayout(); } Window.Content = DataGrid; }
private void grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.Property.Name == "Photo") { var imageCol = new DataGridImageColumn(e.Property); imageCol.CanUserMove = false; imageCol.Header = ""; imageCol.Binding.Converter = new ImageSourceConverter(); imageCol.Binding.ConverterParameter = "/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/tennis/"; e.Column = imageCol; } if (e.Property.Name == "Birthday" && e.Column is DataGridBoundColumn) { ((DataGridBoundColumn)e.Column).Format = "d"; } if (e.Property.Name == "Website") { var hyperCol = new DataGridHyperlinkColumn(e.Property); //hyperCol.CellContentStyle = Resources["HyperlinkColumnStyle"] as Style; e.Column = hyperCol; } }
/// <summary> /// Helper Method which creates a default DataGridColumn object for the specified property type. /// </summary> /// <param name="itemProperty"></param> /// <returns></returns> internal static DataGridColumn CreateDefaultColumn(ItemPropertyInfo itemProperty) { Debug.Assert(itemProperty != null && itemProperty.PropertyType != null, "itemProperty and/or its PropertyType member cannot be null"); DataGridColumn dataGridColumn = null; DataGridComboBoxColumn comboBoxColumn = null; Type propertyType = itemProperty.PropertyType; // determine the type of column to be created and create one if (propertyType.IsEnum) { comboBoxColumn = new DataGridComboBoxColumn(); comboBoxColumn.ItemsSource = Enum.GetValues(propertyType); dataGridColumn = comboBoxColumn; } else if (typeof(string).IsAssignableFrom(propertyType)) { dataGridColumn = new DataGridTextColumn(); } else if (typeof(bool).IsAssignableFrom(propertyType)) { dataGridColumn = new DataGridCheckBoxColumn(); } else if (typeof(Uri).IsAssignableFrom(propertyType)) { dataGridColumn = new DataGridHyperlinkColumn(); } else { dataGridColumn = new DataGridTextColumn(); } // determine if the datagrid can sort on the column or not if (!typeof(IComparable).IsAssignableFrom(propertyType)) { dataGridColumn.CanUserSort = false; } dataGridColumn.Header = itemProperty.Name; // Set the data field binding for such created columns and // choose the BindingMode based on editability of the property. DataGridBoundColumn boundColumn = dataGridColumn as DataGridBoundColumn; if (boundColumn != null || comboBoxColumn != null) { Binding binding = new Binding(itemProperty.Name); if (comboBoxColumn != null) { comboBoxColumn.SelectedItemBinding = binding; } else { boundColumn.Binding = binding; } PropertyDescriptor pd = itemProperty.Descriptor as PropertyDescriptor; if (pd != null) { if (pd.IsReadOnly) { binding.Mode = BindingMode.OneWay; dataGridColumn.IsReadOnly = true; } } else { PropertyInfo pi = itemProperty.Descriptor as PropertyInfo; if (pi != null) { if (!pi.CanWrite) { binding.Mode = BindingMode.OneWay; dataGridColumn.IsReadOnly = true; } } } } return dataGridColumn; }
public void LoadAccountsFromDataBase() { try { LDGlobals.loadedAccountsDictionary.Clear(); LDGlobals.listAccounts.Clear(); DataTable dt = new DataTable(); dt.Columns.Add("UserName"); dt.Columns.Add("Password"); // dt.Columns.Add("ScreenName"); //// dt.Columns.Add("FollowerCount"); // dt.Columns.Add("FollwingCount"); dt.Columns.Add("proxyAddress"); dt.Columns.Add("proxyPort"); dt.Columns.Add("ProxyUserName"); dt.Columns.Add("ProxyPassword"); // dt.Columns.Add("GroupName"); // dt.Columns.Add("AccountStatus"); int counter = 0; DataSet ds = null; try { ds = Qm.SelectAccounts(); } catch (Exception ex) { GlobusLogHelper.log.Error("Error : " + ex.StackTrace); } foreach (DataRow ds_item in ds.Tables[0].Rows) { if (Globals.CheckLicenseManager == "fdfreetrial" && counter == 5) { break; } counter = counter + 1; try { string item = ds_item[0].ToString() + ":" + ds_item[1].ToString() + ":" + ds_item[2].ToString() + ":" + ds_item[3].ToString() + ":" + ds_item[4].ToString() + ":" + ds_item[5].ToString(); string account = item; string[] AccArr = account.Split(':'); if (AccArr.Count() > 1) { string accountUser = account.Split(':')[0]; string accountPass = account.Split(':')[1]; string ScreenName = string.Empty; string FollowerCount = string.Empty; string FollwingCount = string.Empty; string proxyAddress = string.Empty; string proxyPort = string.Empty; string proxyUserName = string.Empty; string proxyPassword = string.Empty; string GroupName = string.Empty; string AccountStatus = string.Empty; string status = string.Empty; DataGridColumn newcolumn = new DataGridHyperlinkColumn(); int DataCount = account.Split(':').Length; if (DataCount == 2) { //Globals.accountMode = AccountMode.NoProxy; } else if (DataCount == 4) { proxyAddress = account.Split(':')[2]; proxyPort = account.Split(':')[3]; } else if (DataCount == 6) { proxyAddress = account.Split(':')[2]; proxyPort = account.Split(':')[3]; proxyUserName = account.Split(':')[4]; proxyPassword = account.Split(':')[5]; } dt.Rows.Add(accountUser, accountPass, proxyAddress, proxyPort, proxyUserName, proxyPassword); try { LinkedinUser objLD_Users = new LinkedinUser(); // TWTUsers objTwtUser = new TWTUsers(); objLD_Users.username = accountUser; objLD_Users.password = accountPass; objLD_Users.proxyip = proxyAddress; objLD_Users.proxyport = proxyPort; objLD_Users.proxyusername = proxyUserName; objLD_Users.proxypassword = proxyPassword; LDGlobals.loadedAccountsDictionary.Add(objLD_Users.username, objLD_Users); // TWTGlobals.loadedAccountsDictionary.Add(objTwtUser.username, objTwtUser); #region MyRegion //try //{ // if (cmbGroups_GroupCampaignManager_Accounts.InvokeRequired) // { // cmbScraper__fanscraper_Accounts.Invoke(new MethodInvoker(delegate // { // cmbScraper__fanscraper_Accounts.Items.Add(accountUser); // })); // } //} //catch (Exception ex) //{ // GlobusLogHelper.log.Error("Error : " + ex.StackTrace); //} //try //{ // if (cmbScraper__CustomAudiencesScraper_Accounts.InvokeRequired) // { // cmbScraper__CustomAudiencesScraper_Accounts.Invoke(new MethodInvoker(delegate // { // cmbScraper__CustomAudiencesScraper_Accounts.Items.Add(accountUser); // })); // } //} //catch (Exception ex) //{ // GlobusLogHelper.log.Error("Error : " + ex.StackTrace); //} //try //{ // //cmbCommentsOnPostSelectAccount // if (cmbCommentsOnPostSelectAccount.InvokeRequired) // { // cmbCommentsOnPostSelectAccount.Invoke(new MethodInvoker(delegate // { // cmbCommentsOnPostSelectAccount.Items.Add(accountUser + ":" + accountPass); // })); // } //} //catch (Exception ex) //{ // GlobusLogHelper.log.Error(ex.Message); //} //try //{ // if (cmbGroups_GroupCampaignManager_Accounts.InvokeRequired) // { // cmbGroups_GroupCampaignManager_Accounts.Invoke(new MethodInvoker(delegate // { // cmbGroups_GroupCampaignManager_Accounts.Items.Add(accountUser); // })); // } //} //catch (Exception ex) //{ // GlobusLogHelper.log.Error("Error : " + ex.StackTrace); //} //try //{ // if (cmbScraper__GroupMemberScraper_Accounts.InvokeRequired) // { // cmbScraper__GroupMemberScraper_Accounts.Invoke(new MethodInvoker(delegate // { // cmbScraper__GroupMemberScraper_Accounts.Items.Add(accountUser); // })); // } //} //catch (Exception ex) //{ // GlobusLogHelper.log.Error("Error : " + ex.StackTrace); //} #endregion LDGlobals.listAccounts.Add(objLD_Users.username + ":" + objLD_Users.password + ":" + objLD_Users.proxyip + ":" + objLD_Users.proxyport + ":" + objLD_Users.proxyusername + ":" + objLD_Users.proxypassword); // TWTGlobals.listAccounts.Add(objTwtUser.username + ":" + objTwtUser.password + ":" + objTwtUser.proxyip + ":" + objTwtUser.proxyport + ":" + objTwtUser.proxyusername + ":" + objTwtUser.proxypassword); } catch (Exception ex) { GlobusLogHelper.log.Error("Error : " + ex.StackTrace); } ///Set this to "0" if loading unprofiled accounts /// string profileStatus = "0"; } else { GlobusLogHelper.log.Info("Account has some problem : " + item); GlobusLogHelper.log.Debug("Account has some problem : " + item); } } catch (Exception ex) { GlobusLogHelper.log.Error("Error : " + ex.StackTrace); } } DataView dv = dt.DefaultView; dv.AllowNew = false; this.Dispatcher.Invoke(new Action(delegate { // grvAccounts_AccountCreator_AccountDetails.ItemsSource = dv; })); try { grvAccounts_AccountCreator_AccountDetails.ItemsSource = dt.DefaultView; } catch { } GlobusLogHelper.log.Debug("Accounts Loaded : " + dt.Rows.Count); GlobusLogHelper.log.Info("Accounts Loaded : " + dt.Rows.Count); } catch (Exception ex) { GlobusLogHelper.log.Error("Error : " + ex.StackTrace); } }
public void Init(IEnumerable <object> bindingData, EventHandler callBack = null) { // 目前不处理空数据 if (null == bindingData || 0 == bindingData.Count()) { this.sp_emptyDataNotice.Visibility = Visibility.Visible; this.dg_dynamic.Visibility = Visibility.Hidden; return; //throw new Exception("动态DataGrid初始化的数据不能为空!"); } this.sp_emptyDataNotice.Visibility = Visibility.Hidden; this.dg_dynamic.Visibility = Visibility.Visible; ClearGird(); s_allDatas = bindingData; m_callBackHandler = callBack; var type = bindingData.ElementAt(0).GetType(); var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var dynamicSearchColAttr = type.GetCustomAttribute <DynamicDataGridSearchColAttribute>(); var keyColName = dynamicSearchColAttr?.KeyColumn; var searchColDic = dynamicSearchColAttr.ExplainSearchColumnSettings(); string colHeader = ""; // 根据配置,找出显示的查询属性 var selProperties = from pro in properties where searchColDic.Keys.Contains(pro.Name) select pro; if (null == selProperties || 0 == selProperties.Count()) { return; } PropertyInfo keyColumnProperty = null; if (!string.IsNullOrEmpty(keyColName)) { keyColumnProperty = (from pro in selProperties where pro.Name == keyColName select pro).First(); } else { // 如果没有配置关键列,则取第一列 keyColumnProperty = selProperties.First(); keyColName = keyColumnProperty.Name; } s_primaryProperty = keyColumnProperty; var choiceCol = new DataGridHyperlinkColumn(); choiceCol.Header = "操作"; choiceCol.Binding = new Binding("SelectText"); choiceCol.Width = DataGridLength.Auto; choiceCol.MinWidth = 60; choiceCol.IsReadOnly = true; dg_dynamic.Columns.Add(choiceCol); foreach (var ePro in selProperties) { var proName = ePro.Name; colHeader = searchColDic[proName]; var newCol = new DataGridTextColumn(); newCol.Header = colHeader; newCol.Binding = new Binding(proName); newCol.MinWidth = 100; newCol.IsReadOnly = true; dg_dynamic.Columns.Add(newCol); } dg_dynamic.ItemsSource = bindingData; foreach (var eData in bindingData) { var key = keyColumnProperty.GetValue(eData).ToString(); AllKeyList.Add(key); KeyObjDic.Add(key, eData); } acb_search.ItemsSource = AllKeyList; }
private void AddNewColumnInDataGrid(DataTable dataTable, string attributeName, string dataColumnName) { var dataColumn = dataTable.Columns[dataColumnName]; if (dataColumn.DataType == typeof(EntityReferenceView) || dataColumn.DataType == typeof(PrimaryGuidView) ) { var columnDGT = new DataGridHyperlinkColumn() { Header = attributeName.Replace("_", "__"), Width = DataGridLength.Auto, SortMemberPath = dataColumnName, ContentBinding = new Binding("[" + dataColumnName + "]") { Mode = BindingMode.OneTime, }, Binding = new Binding("[" + dataColumnName + "].Url") { Mode = BindingMode.OneTime, }, }; lstVwEntities.Columns.Add(columnDGT); } else { string format = null; if (dataColumn.DataType == typeof(decimal) || dataColumn.DataType == typeof(double) ) { format = "{0:F2}"; } else if (dataColumn.DataType == typeof(DateTime)) { format = "{0:G}"; } var columnDGT = new DataGridTextColumn() { Header = attributeName.Replace("_", "__"), Width = DataGridLength.Auto, SortMemberPath = dataColumnName, Binding = new Binding(dataColumnName) { Mode = BindingMode.OneTime, StringFormat = format, ConverterCulture = CultureInfo.CurrentCulture, }, }; lstVwEntities.Columns.Add(columnDGT); } }