将动态数据绑定到Silverlight Datagrid c#

时间:2014-08-23 14:59:57

标签: c# wpf silverlight dictionary datagrid

我正在尝试使用绑定到Dictionary的动态数据构建数据网格,但生成的数据网格包含空行。

XAML

   <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid AutoGenerateColumns="False" Height="153" HorizontalAlignment="Left" Margin="0,75,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="400"  />
    </Grid>

CodeBehind.cs

  Dictionary<string, object> d = new Dictionary<string, object>();
    public MainPage()
    {
        InitializeComponent();
        List<Dictionary<string, object>> theList = new List<Dictionary<string, object>>();
        for(int i=0;i<10;i++)
        {
        d = new Dictionary<string, object>();
        d.Add("columnName1", "John");
        d.Add("columnName2", "Smith");
        d.Add("columnName3", 29);
        d.Add("columnName", 5.9);
        theList.Add(d);
        }
        this.AddColumnToGrid(dataGrid1, (IEnumerable<Dictionary<string, object>>)theList);
        dataGrid1.ItemsSource = theList;
     }
    private void AddColumnToGrid(DataGrid theGrid, IEnumerable<Dictionary<string, object>> IEnumDataList)
    {
     Dictionary<string, object> firstRow = (Dictionary<string, object>)IEnumDataList.FirstOrDefault();
     foreach (KeyValuePair<string, object> pair in firstRow)
     {
      theGrid.Columns.Add(CreateColumn(pair.Key));
     }
    }
    RowIndexConverter _rowIndexConverter = new RowIndexConverter();
    private DataGridColumn CreateColumn(string property)
    {
        return new DataGridTextColumn()
        {
            CanUserSort = true,
            Header = property,
            SortMemberPath = property,
            IsReadOnly = false,
            Binding = new Binding()
            {
                Converter = _rowIndexConverter,
                ConverterParameter = property,
                Mode = BindingMode.OneWay
            }
        };
    }
    public class Row
    {
        private Dictionary<string, object> _data = new Dictionary<string, object>();

        public object this[string index]
        {
            get { return _data[index]; }
            set { _data[index] = value; }
        }
    }
    public class RowIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
        {
            SilverlightApplication1.MainPage.Row row = value as SilverlightApplication1.MainPage.Row;
            if (row == null)
            {
                return null;
            }
            string index = parameter as string;
            return row[index];
        }
        public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

1 个答案:

答案 0 :(得分:1)

问题在于您的转换器。你将它投射到Row虽然它应该被铸造到Disctionary。要解决此问题,请在Row类中创建一个构造函数,该构造函数将Dictionary作为参数,如

        public class Row
    {
        public Row(Dictionary<string, object> data)
        {
            this._data = data;
        }

        private Dictionary<string, object> _data = new Dictionary<string, object>();

        public object this[string index]
        {
            get { return _data[index]; }
            set { _data[index] = value; }
        }
    }
  

在RowIndexConverter中转换为Dictionary,如

        public class RowIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var dict = value as Dictionary<string, object>;
            if (dict != null)
            {
                Row row = new  Row(dict);
                string index = parameter as string;
                return row[index];
            }
            return null;
        }

 }
  

Solution2 我认为为每个Convert创建Row类对象并不方便。您可以直接在ConvertMethod中执行此操作,如

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var dict = value as Dictionary<string, object>;
            object val;
            if (dict != null && parameter!=null && dict.TryGetValue(parameter.ToString(), out val))
                return val;
            return null;
        }

Output