Dynamic generate column mvvm

前端 未结 2 1676
感情败类
感情败类 2020-12-04 22:20

I try to make a ListView with dynamic generation of column. I use mvvm patern. How i can implement this? In this momemt I have only static columns.



        
2条回答
  •  萌比男神i
    2020-12-04 22:57

    You can create GridView with appropriate columns dynamically using converter. Here is working example:

    App screen shot

    MainWindow.xaml

    
        
            
        
            
    
    

    MainWindow.xaml.cs

    using System.Collections.Generic;
    using System.Windows;
    
    namespace WpfApplication1
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new ViewModel();
            }
        }
    
        public class ViewModel
        {
            public ColumnConfig ColumnConfig { get; set; }
            public IEnumerable Products { get; set; }
    
            public ViewModel()
            {
                Products = new List { new Product { Name = "Some product", Attributes = "Very cool product" }, new Product { Name = "Other product", Attributes = "Not so cool one" } };
                ColumnConfig = new ColumnConfig { Columns = new List { new Column { Header = "Name", DataField = "Name" }, new Column { Header = "Attributes", DataField = "Attributes" } } };
            }
        }
    
        public class ColumnConfig
        {
            public IEnumerable Columns { get; set; }
        }
    
        public class Column
        {
            public string Header { get; set; }
            public string DataField { get; set; }
        }
    
        public class Product
        {
            public string Name { get; set; }
            public string Attributes { get; set; }
        }
    }
    

    ConfigToDynamicGridViewConverter.cs

    using System;
    using System.Globalization;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace WpfApplication1
    {
        public class ConfigToDynamicGridViewConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var config = value as ColumnConfig;
                if (config != null)
                {
                    var grdiView = new GridView();
                    foreach (var column in config.Columns)
                    {
                        var binding = new Binding(column.DataField);
                        grdiView.Columns.Add(new GridViewColumn {Header = column.Header, DisplayMemberBinding = binding});
                    }
                    return grdiView;
                }
                return Binding.DoNothing;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotSupportedException();
            }
        }
    }
    

提交回复
热议问题