DataGrid某一列绑定数组问题
一个DataGrid的Itemsource绑定了一个List<User>,
并Bing了User的所有列,
但User中有一个字段为string[] str,
怎么控制DataGrid显示这个数组为str[0];str[1];str[2]......
[解决办法]
不知道是不是你需要的。。
namespace DataBindingSimple{ public partial class StringArrayBinding : UserControl { public StringArrayBinding() { InitializeComponent(); List<Data> dataList = new List<Data> { new Data{ID=0, str=new string[3]{"I", "like", "silverlight"}}, new Data{ID=1, str=new string[4]{"Do", "you", "like", "it?"}}, }; dataGrid1.ItemsSource = dataList; } } public class Data { public int ID { get; set; } public string[] str { get; set; } }//定义converter, public class StringArrayToStringConverter : System.Windows.Data.IValueConverter { //source => target 转变, 即string[] => string //参数中value为source值, 返回值为target值 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return String.Join(" ", (string[])value); } //target => source转变, 即string =>string[], 没有实现。 //用TwoWay binding时必须实现 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } }}