Reputation: 24949
I am binding a DataGrid to a DataSet on my ViewModel.
I would like to switch to a more light weight control such as ListView.GridView, but it doesn't appear it supports dynamic column generation.
Since this is a MVVM model, i am trying to avoid having to loop through my dataset columns, and add each GridViewColumn from a codebehind.
is it possible to 1. Bind ad a Listview to a DataSet w/out setting columns explicitly, or 2. Bind the Columns collection to a property on the ViewModel.
If not, what are some other free grid controls that would allow this and outperform DataGrid?
Upvotes: 0
Views: 4402
Reputation: 1
Assume DataSet1:
Customer
Name
Address
Phone
Model:
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: (combined with View here for 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;
}
}
View:
<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>
Upvotes: -2
Reputation: 2556
Dont believe you can do it easily, i would personally use the IValueConverter interface to convert your DataSet to an object that you can manage your columns inside.
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 Code
<conv:DataSetConverter x:key="datasetConverter"/>
<ListBox x:Name="listbox1" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding datasetObject, Converter={StaticResource datasetConverter}}" >
Upvotes: 0