How to define a DataGrid using ListView in WPF?

来源:百度文库 编辑:神马文学网 时间:2024/06/30 22:10:36
Ifyou want to have a very nice looking DataGrid you have to use ListViewcontrol. It enables you to use ControlTemplate for both the header andthe column and also has built in functionality for scrolling throughthe list. So here is how you can build a DataGrid using ListView.
First you need to define the ListView:

                                                                                                                                                                                                              
                   
                                                  






                                                     





Asyou may have noticed I added one event handler for MouseDoubleClickevent, That is how you can enable selection feature in the list by justsetting the MouseDoubleClick or any other event on the ListView.
And this is what the select event handler looks like:
void Selected(object sender, RoutedEventArgs e)
{
ListView cmd = (ListView)sender;
string selectedItem = (string)(((System.Data.DataRowView)(cmd.SelectedItem)).Row[0]);
}
Itjust seems easier to define Style as resource for me, so here is thestyle for the ListView. I need to note that if your list is too big youwill need to enable the Scroll feature enable. This is how you shoulddo this: just set ScrollViewer.HorizontalScrollBarVisibility toVisible. The ScrollViewer embedded in the ListView will use thisproperty accordingly.








Nowyou have a very nice DataGrid. If you are interested to make yourdatagrid’s look more interesting, you can use ControlTemplate like whatI am using here:

TextBlock.TextAlignment="Center" Width="{TemplateBinding  Width}"
BorderBrush="Chocolate" Background="AntiqueWhite"  >



And of course you can apply this template through the style of the Header or column.