【问题标题】:Bind Dynamic Data to Silverlight Datagrid c#将动态数据绑定到 Silverlight Datagrid c#
【发布时间】:2014-10-17 06:22:37
【问题描述】:

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

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();
        }
    }

【问题讨论】:

    标签: c# wpf silverlight dictionary datagrid


    【解决方案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 中转换为字典,如

            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;
            }
    

    【讨论】:

    • @谢谢兄弟,还有一个问题,我需要在填充数据后向数据网格添加新行。我怎样才能做到这一点?我应该也能得到更新的数据。
    • 很高兴能帮到你。对于那个帖子,我认为另一个问题不会那么容易。
    猜你喜欢
    • 2011-04-13
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多