guys!
I currently have this WPF Project With WCF and LINQ :
MainWindow.xaml
<GroupBox Header="Available Rooms" >
<DataGrid Name="roomDataGrid" ItemsSource="{Binding Rooms}" AutoGenerateColumns ="False" ">
<DataGrid.Columns>
<DataGridTextColumn Header="Room" Binding="{Binding RoomId}" />
<DataGridTextColumn Header="Reserved" Binding="{Binding RoomTaken}" />
<DataGridTextColumn Header="Beds" Binding="{Binding Beds}" />
<DataGridTextColumn Header="Size" Binding="{Binding Size}" />
<DataGridTextColumn Header="Rank" Binding="{Binding RoomRank}" />
</DataGrid.Columns>
</DataGrid>
</GroupBox>
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
viewModel = new ViewModel();
this.DataContext = viewModel;
}
ViewModel.cs
public class ViewModel
{
HotelService.HotelServiceClient hotelServiceClient = new HotelService.HotelServiceClient();
public List<RoomModel> Rooms
{
get
{
return hotelServiceClient.GetRooms().ToList();
}
}
}
So as you guys can see, I just set this.DataContext = viewModel and almost through sheer Magic, the WPF shows the database list of Rooms.
But my question is as following. Is there anyway I can achieve the same thing using a GridView/Listview ? I tried replacing the xaml with those Objects, but then it didn't show anything in the window.
Bonus question : Is there anyway to use GridView/ListView in accordance with events/delegates, so that I can save Rooms to DB and get the view updated, and also get the view updated when there are changes done to the DB that are not caused by this WPF?
I hope I supplied enough information! And thanks in advance :)
Try this:
<GroupBox Header="Available Rooms" >
<ListView Name="roomDataGrid" ItemsSource="{Binding Rooms}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Room" DisplayMemberBinding="{Binding RoomId}" />
<GridViewColumn Header="Reserved" DisplayMemberBinding="{Binding RoomTaken}" />
<GridViewColumn Header="Beds" DisplayMemberBinding="{Binding Beds}" />
<GridViewColumn Header="Size" DisplayMemberBinding="{Binding Size}" />
<GridViewColumn Header="Rank" DisplayMemberBinding="{Binding RoomRank}" />
</GridView>
</ListView.View>
</ListView>