WPF Listview GridView绑定:动态列

时间:2011-03-24 23:46:21

标签: c# wpf listview gridview datagrid

我将DataGrid绑定到ViewModel上的DataSet。

我想切换到更轻量级的控件,如ListView.GridView,但它似乎不支持动态列生成。

由于这是一个MVVM模型,我试图避免遍历我的数据集列,并从代码隐藏中添加每个GridViewColumn。

是否可以1.将Listview广告绑定到DataSet w / out明确设置列,或2.将Columns集合绑定到ViewModel上的属性。

如果没有,那么有哪些其他免费网格控件可以实现这一目标并且优于DataGrid?

2 个答案:

答案 0 :(得分:0)

不要相信你可以轻松完成,我个人会使用IValueConverter接口将你的DataSet转换为一个可以管理你内部列的对象。

public sealed class DataSetConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
           if((DataSet)value != null)
{

// Put logic in here to loop through the columns and create an object to bind to the ListView control.

}
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

XAML代码

 <conv:DataSetConverter x:key="datasetConverter"/>
                <ListBox x:Name="listbox1" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                        ItemsSource="{Binding datasetObject, Converter={StaticResource datasetConverter}}" >

答案 1 :(得分:-2)

假设DataSet1:

Customer
  Name
  Address
  Phone

型号:

namespace WpfApplication1.Model
{
    class MyDataSetModel
    {
        private DataSet1 _myDataSet;
        private DataSet1TableAdapters.CustomerTableAdapter _myCustomerTableAdapter;
        public DataSet1.CustomerDataTable Customers
        {
            get { return _myDataSet.Customer; }   
        }

        public void FetchCustomers()
        {
            _myDataSet = new DataSet1();
            _myCustomerTableAdapter = new CustomerTableAdapter();
            _myCustomerTableAdapter.Fill(_myDataSet.Customer);
        }
    }
}

ViewModel :(结合此处查看clairity)

public partial class Window1 : Window
{
    private MyDataSetModel _myDataModel;
    public Window1()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        _myDataModel = new MyDataSetModel();
        _myDataModel.FetchCustomers();

        listView1.ItemsSource = _myDataModel.Customers;
    }
}

查看:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="400" Width="600">
    <Grid>
        <ListView Name="listView1">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding= "{Binding Path=Name}">
                        <GridViewColumnHeader Width="100">Name</GridViewColumnHeader>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding= "{Binding Path=Address}">
                        <GridViewColumnHeader Width="340">Address</GridViewColumnHeader>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding= "{Binding Path=Phone}">
                        <GridViewColumnHeader Width="100">Phone</GridViewColumnHeader>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>