public void Test___Method_Convert___Generic_2() { var testee = new DictionaryConverter <int, string>() { Dictionary = new Dictionary <int, string>() { { 1, "One" }, { 2, "Two" }, { 3, "Three" } }, Default = "Many" }; Assert.AreEqual("One", testee.Convert(1)); Assert.AreEqual("Two", testee.Convert(2)); Assert.AreEqual("Three", testee.Convert(3)); Assert.AreEqual("Many", testee.Convert(4)); }
public ObjectControl() { converterChanges .CombineLatest(objectChanges, comparerChanges, textBlockChanges, filterChanges, (valueConverter, value, comparer, textBlock, filter) => (valueConverter, value, comparer, textBlock, filter)) .Subscribe(valueTuple => { var(valueConverter, value, comparer, textBlock, filter) = valueTuple; Task.Run(() => value switch { string _ => (Visibility.Collapsed, value), Version _ => (Visibility.Collapsed, value), { } x when x.GetType().IsClass == false => (Visibility.Collapsed, value), null => (Visibility.Collapsed, value), IEnumerable <string> _ => (Visibility.Collapsed, value), IEnumerable enumerable when enumerable.NotOfClassType() => (Visibility.Collapsed, enumerable), IEnumerable enumerable when enumerable.OfSameType() => (Visibility.Collapsed, enumerable), IEnumerable enumerable => DictionaryConverter.ConvertMany(enumerable, valueConverter, filter, comparer) .Pipe(a => (a.Keys.Cast <object>().Any() ? Visibility.Visible : Visibility.Collapsed, a)), _ => DictionaryConverter.Convert(value, valueConverter, filter, comparer) .Pipe(a => (a.Keys.Cast <object>().Any() ? Visibility.Visible : Visibility.Collapsed, a)) }) .ToObservable() .ObserveOnDispatcher() .Subscribe(a => { var(visibility, content) = a; textBlock.SetValue(VisibilityProperty, visibility); this.SetValue(ContentProperty, content); }, e => { SetValue(ContentProperty, new OrderedDictionary(1) { { "Error", e.Message } }); this.Log().Write(e, $"Error in {nameof(ObjectControl)} creating content", typeof(ObjectControl), LogLevel.Error); }, () => { }); });
//public static void Export2CSV(DataGrid dtGrid, IEnumerable ItemsSource,string fileName) //{ // string csvStr = ConverDataSet2CSV(dtGrid,ItemsSource); // if(csvStr=="") return; // FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); // //将string转换成byte[] // byte[] csvArray = System.Text.Encoding.UTF8.GetBytes(csvStr.ToCharArray(), 0, csvStr.Length - 1); // fs.Write(csvArray,0,csvStr.Length - 1); // fs.Close(); // fs = null; //} ///// <summary> ///// 将指定的数据集中指定的表转换成CSV字符串 ///// </summary> ///// <param name="dataGrid"></param> ///// <param name="tableName"></param> ///// <returns></returns> //private static string ConverDataSet2CSV(DataGrid dataGrid, IEnumerable ItemsSource) //{ // //首先判断数据集中是否包含指定的表 // if (dataGrid == null || dataGrid.ItemsSource==null) // { // MessageBox.Show("指定的数据集为空或不包含要写出的数据表!"); // return ""; // } // string csvStr = ""; // //下面写出数据 // //DataTable tb = dataGrid.Tables[tableName]; // //写表名 // //csvStr += tb.TableName + "\n"; // //第一步:写出列名 // foreach (var column in dataGrid.Columns) // { // if(column is DataGridTextColumn) // { // DataGridTextColumn item=column as DataGridTextColumn; // csvStr += "\""+column.Header.ToString() +"\"" +","; // } // } // //去掉最后一个"," // csvStr = csvStr.Remove(csvStr.LastIndexOf(","), 1); // csvStr += "\n"; // //第二步:写出数据 // foreach (var row in ItemsSource) // { // if(row is DateTime) // { // CustomDateConverter converter=new CustomDateConverter(); // converter.Convert(row,"DATE"); // } // foreach (DataColumn column in tb.Columns) // { // csvStr += "\"" + row[column].ToString() + "\"" + ","; // } // csvStr = csvStr.Remove(csvStr.LastIndexOf(","), 1); // csvStr += "\n"; // } // return csvStr; //} #endregion /// <summary> /// 导出DataGrid数据到Excel /// </summary> /// <param name="withHeaders">是否需要表头</param> /// <param name="grid">DataGrid</param> /// <returns>Excel内容字符串</returns> public static string ExportDataGrid(bool withHeaders, DataGrid grid) { System.Reflection.PropertyInfo propInfo; System.Windows.Data.Binding binding; var strBuilder = new System.Text.StringBuilder(); DictionaryConverter dicConverter = new DictionaryConverter(); var source = (grid.ItemsSource as System.Collections.IList); if (source == null) return ""; var headers = new List<string>(); grid.Columns.ForEach(col => { if (col is DataGridBoundColumn) { string strHeader = ConvertDic(col.Header.ToString()); headers.Add(FormatCsvField(strHeader)); } }); strBuilder.Append(String.Join(",", headers.ToArray())).Append("\r\n"); foreach (Object data in source) { var csvRow = new List<string>(); foreach (DataGridColumn col in grid.Columns) { try { if (col is DataGridBoundColumn) { binding = (col as DataGridBoundColumn).Binding; string colPath = binding.Path.Path; string[] arr = colPath.Split('.'); string dicCategory = Convert.ToString(binding.ConverterParameter);//如有绑定字典值,则为字典类别 propInfo = data.GetType().GetProperty(colPath); object ob = data; if (arr.Length > 1) { ob = data.GetObjValue(arr[0]); propInfo = data.GetObjValue(arr[0]).GetType().GetProperty(arr[1]); } if (propInfo != null) { object obj = propInfo.GetValue(ob, null) == null ? null : propInfo.GetValue(ob, null).ToString(); obj = dicConverter.Convert(obj, null, dicCategory, null); string value = Convert.ToString(obj); csvRow.Add(FormatCsvField(value)); } } } catch { continue; } } strBuilder.Append(String.Join(",", csvRow.ToArray())).Append("\r\n"); } return strBuilder.ToString(); }
//public static void Export2CSV(DataGrid dtGrid, IEnumerable ItemsSource,string fileName) //{ // string csvStr = ConverDataSet2CSV(dtGrid,ItemsSource); // if(csvStr=="") return; // FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); // //将string转换成byte[] // byte[] csvArray = System.Text.Encoding.UTF8.GetBytes(csvStr.ToCharArray(), 0, csvStr.Length - 1); // fs.Write(csvArray,0,csvStr.Length - 1); // fs.Close(); // fs = null; //} ///// <summary> ///// 将指定的数据集中指定的表转换成CSV字符串 ///// </summary> ///// <param name="dataGrid"></param> ///// <param name="tableName"></param> ///// <returns></returns> //private static string ConverDataSet2CSV(DataGrid dataGrid, IEnumerable ItemsSource) //{ // //首先判断数据集中是否包含指定的表 // if (dataGrid == null || dataGrid.ItemsSource==null) // { // MessageBox.Show("指定的数据集为空或不包含要写出的数据表!"); // return ""; // } // string csvStr = ""; // //下面写出数据 // //DataTable tb = dataGrid.Tables[tableName]; // //写表名 // //csvStr += tb.TableName + "\n"; // //第一步:写出列名 // foreach (var column in dataGrid.Columns) // { // if(column is DataGridTextColumn) // { // DataGridTextColumn item=column as DataGridTextColumn; // csvStr += "\""+column.Header.ToString() +"\"" +","; // } // } // //去掉最后一个"," // csvStr = csvStr.Remove(csvStr.LastIndexOf(","), 1); // csvStr += "\n"; // //第二步:写出数据 // foreach (var row in ItemsSource) // { // if(row is DateTime) // { // CustomDateConverter converter=new CustomDateConverter(); // converter.Convert(row,"DATE"); // } // foreach (DataColumn column in tb.Columns) // { // csvStr += "\"" + row[column].ToString() + "\"" + ","; // } // csvStr = csvStr.Remove(csvStr.LastIndexOf(","), 1); // csvStr += "\n"; // } // return csvStr; //} #endregion /// <summary> /// 导出DataGrid数据到Excel /// </summary> /// <param name="withHeaders">是否需要表头</param> /// <param name="grid">DataGrid</param> /// <returns>Excel内容字符串</returns> public static string ExportDataGrid(bool withHeaders, DataGrid grid) { System.Reflection.PropertyInfo propInfo; System.Windows.Data.Binding binding; var strBuilder = new System.Text.StringBuilder(); DictionaryConverter dicConverter = new DictionaryConverter(); var source = (grid.ItemsSource as System.Collections.IList); if (source == null) { return(""); } var headers = new List <string>(); grid.Columns.ForEach(col => { if (col is DataGridBoundColumn) { string strHeader = ConvertDic(col.Header.ToString()); headers.Add(FormatCsvField(strHeader)); } }); strBuilder.Append(String.Join(",", headers.ToArray())).Append("\r\n"); foreach (Object data in source) { var csvRow = new List <string>(); foreach (DataGridColumn col in grid.Columns) { try { if (col is DataGridBoundColumn) { binding = (col as DataGridBoundColumn).Binding; string colPath = binding.Path.Path; string[] arr = colPath.Split('.'); string dicCategory = Convert.ToString(binding.ConverterParameter);//如有绑定字典值,则为字典类别 propInfo = data.GetType().GetProperty(colPath); object ob = data; if (arr.Length > 1) { ob = data.GetObjValue(arr[0]); propInfo = data.GetObjValue(arr[0]).GetType().GetProperty(arr[1]); } if (propInfo != null) { object obj = propInfo.GetValue(ob, null) == null ? null : propInfo.GetValue(ob, null).ToString(); obj = dicConverter.Convert(obj, null, dicCategory, null); string value = Convert.ToString(obj); csvRow.Add(FormatCsvField(value)); } } } catch { continue; } } strBuilder.Append(String.Join(",", csvRow.ToArray())).Append("\r\n"); } return(strBuilder.ToString()); }