? ? ? ?ListView 控件在Windows應(yīng)用程序中常用,用于表示數(shù)據(jù)列表。如果您以前使用過 WinForms,那么您對(duì)ListView的實(shí)用性有一個(gè)很好的了解,但您應(yīng)該意識(shí)到 WPF中的ListView 不像WinForms版本那樣使用。再一次的主要區(qū)別在于,雖然WinForms ListView只是調(diào)用Windows API 函數(shù)來呈現(xiàn)常見的Windows ListView 控件,但WPF ListView是一個(gè)獨(dú)立的控件,不依賴于 Windows API,WPF ListView以完全不同的方式處理項(xiàng)目圖像、組及其子項(xiàng)目等內(nèi)容。事實(shí)上,它看起來很像 WPF ListBox,直到您開始向它添加專門的視圖。這并不奇怪,因?yàn)?ListView 直接從 ListBox 控件繼承。因此,默認(rèn)的ListView實(shí)際上只是一個(gè)ListBox,具有不同的選擇模式。
今天開搞做一個(gè)比較綜合的ListView示例(會(huì)要用到.net中的EF知識(shí)),主要包括以下功能:?
1)?查詢功能。從數(shù)據(jù)庫ucdos中的CarInfo表中讀取車輛位置信息數(shù)據(jù),然后展示到ListView上。
2)?數(shù)據(jù)聯(lián)動(dòng)功能。當(dāng)鼠標(biāo)左鍵選中ListView中的某一條記錄時(shí),在ListView框下面文本框中顯示詳細(xì)信息。
3)?修改功能。修改TextBox中的內(nèi)容后,點(diǎn)擊“更新”按鈕,把修改后的數(shù)據(jù)保存到數(shù)據(jù)庫中,同時(shí)與Listview聯(lián)動(dòng)
1、在SqlServer中創(chuàng)建數(shù)據(jù)庫ucdos,創(chuàng)建表CarInfo,結(jié)構(gòu)及內(nèi)容如下:
2、在項(xiàng)目中添加文件夾control,然后添加窗口ListViewWin,并在App.xaml文件中設(shè)置啟動(dòng)文件為L(zhǎng)istViewWin.xaml?
3、項(xiàng)目上右鍵,添加->新建項(xiàng),?
最后在項(xiàng)目中出現(xiàn)EF相關(guān)的文件及類,如圖?
?同時(shí)在項(xiàng)目的app.config自動(dòng)添加了數(shù)據(jù)庫的連接配置信息
?這里使用的是.NET中的Entity Framework 6技術(shù),不清楚這塊的請(qǐng)自備這方面知識(shí)
4、開始布局,用到了WrapPanel,StackPanel控件
說明:
如果是你經(jīng)常做ASP.NET或是windows的開發(fā),可能會(huì)形成一個(gè)習(xí)慣——直接從工具箱上把控件拖拽到需要的地方。?這樣直接拖放控件的方式,VS會(huì)自動(dòng)生成一個(gè)固定的坐標(biāo),但是這種方式在WPF中不推薦使用,在WPF中推薦使用? Canvas、StackPanel、WrapPanel、DockPanel和Grid面板組合使用來組裝界面。?
對(duì)于WPF的界面布局,有以下幾點(diǎn)建議:
第一、控件的布局應(yīng)該有容器來決定,而不是通過控件的margin屬性來控制位置。?
第二、控件應(yīng)避免明確的定義具體的尺寸,因?yàn)轱@示器分辨率及windows窗體的大小都有可能隨時(shí)改變,如果明確的定義尺寸。?
當(dāng)窗體變動(dòng)后就會(huì)出現(xiàn)大面積的空白或是缺失。但為了控件功能及效果的展示,應(yīng)該限定一個(gè)可接受的最大及最小尺寸。?
通過MinWidth, MinHeight, MaxWidth, MaxHeight屬性可以實(shí)現(xiàn)這一點(diǎn)。
第三、由于現(xiàn)在顯示器分辨率非常多(1366×768、1600×900、、1980×1080等等),如果將界面元素位置設(shè)置成與屏幕坐標(biāo)相關(guān),這樣做的話是會(huì)有風(fēng)險(xiǎn)的。?
第四、容器應(yīng)將有效空間共享給其子控件,這也是為了不在窗體調(diào)整后,遺留出大塊的空余。?
第五、容器嵌套使用,因?yàn)椴煌娜萜鳎憩F(xiàn)效果不同,必要時(shí)應(yīng)結(jié)合使用。
最外面的Grid容器分為三行,每行分別放listview,WrapPanel,WrapPanel,具體代碼如下:
<Window x:Class="WpfApp6.control.ListViewWin"
? ? ? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? ? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
? ? ? ? xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
? ? ? ? xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
? ? ? ? xmlns:local="clr-namespace:WpfApp6.control"
? ? ? ? mc:Ignorable="d"
? ? ? ? Title="ListViewWin" Height="450" Width="800">
? ? <Grid>
? ? ? ? <Grid.RowDefinitions>
? ? ? ? ? ? <RowDefinition Height="*"/>
? ? ? ? ? ? <RowDefinition Height="Auto"/>
? ? ? ? ? ? <RowDefinition Height="22"/>
? ? ? ? </Grid.RowDefinitions>
? ? ? ? <ListView Grid.Row="0" Name="listView1" HorizontalAlignment="Left" Height="180" VerticalAlignment="Top" Width="792">
? ? ? ? ? ? <ListView.View>
? ? ? ? ? ? ? ? <GridView x:Name="gridView1">
? ? ? ? ? ? ? ? ? ? <!--DisplayMemberBinding屬性的值綁定到后臺(tái)對(duì)象的屬性。-->
? ? ? ? ? ? ? ? ? ? <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
? ? ? ? ? ? ? ? ? ? <GridViewColumn Header="車牌號(hào)" DisplayMemberBinding="{Binding Path=CarNo}"></GridViewColumn>
? ? ? ? ? ? ? ? ? ? <GridViewColumn Header="經(jīng)度" DisplayMemberBinding="{Binding Path=Lng}"></GridViewColumn>
? ? ? ? ? ? ? ? ? ? <GridViewColumn Header="緯度" DisplayMemberBinding="{Binding Path=Lat}"></GridViewColumn>
? ? ? ? ? ? ? ? ? ? <!--StringFormat格式化時(shí)間-->
? ? ? ? ? ? ? ? ? ? <GridViewColumn Header="上傳時(shí)間" >
? ? ? ? ? ? ? ? ? ? ? ? <!--模板化單元格內(nèi)容-->
? ? ? ? ? ? ? ? ? ? ? ? <GridViewColumn.CellTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <TextBlock Foreground="CornflowerBlue" Cursor="Hand" Text="{Binding Path=LocDt,StringFormat='yyyy年MM月dd日HH時(shí)mm分ss秒'}"></TextBlock>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? </GridViewColumn.CellTemplate>
? ? ? ? ? ? ? ? ? ? </GridViewColumn>?
? ? ? ? ? ? ? ? </GridView>
? ? ? ? ? ? </ListView.View>?
? ? ? ? </ListView>
? ? ? ? <!--DataContext指定數(shù)據(jù)上下文,綁定到上面的listView控件上-->
? ? ? ? <WrapPanel Grid.Row="1" ?DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Orientation="Horizontal" Height="100" Margin="0,0,0,0">
? ? ? ? ? ? <StackPanel Orientation="Horizontal" Margin="5,2,5,2">
? ? ? ? ? ? ? ? <TextBlock Name="tbId" Text="ID號(hào):" />
? ? ? ? ? ? ? ? <TextBox Name="txId" IsReadOnly="True" Text="{Binding ID}" MinWidth="100" />
? ? ? ? ? ? </StackPanel>
? ? ? ? ? ? <StackPanel Orientation="Horizontal" Margin="5,2,5,2">
? ? ? ? ? ? ? ? <TextBlock Name="tbCarNo" Text="車牌號(hào):" />
? ? ? ? ? ? ? ? <TextBox Name="txCarNo" Text="{Binding CarNo}" MinWidth="100" />
? ? ? ? ? ? </StackPanel>
? ? ? ? ? ? <StackPanel Orientation="Horizontal" Margin="5,2,5,2">
? ? ? ? ? ? ? ? <TextBlock Name="tbLng" Text="經(jīng)度:" />
? ? ? ? ? ? ? ? <TextBox Name="txLng" Text="{Binding Lng}" MinWidth="100" />
? ? ? ? ? ? </StackPanel>
? ? ? ? ? ? <StackPanel Orientation="Horizontal" Margin="5,2,5,2">
? ? ? ? ? ? ? ? <TextBlock Name="tbLat" Text="緯度:" />
? ? ? ? ? ? ? ? <TextBox Name="txLat" Text="{Binding Lat}" ?MinWidth="100" />
? ? ? ? ? ? </StackPanel>
? ? ? ? ? ? <StackPanel Orientation="Horizontal" Margin="5,2,5,2">
? ? ? ? ? ? ? ? <TextBlock Name="tbLocDt" Text="上傳時(shí)間:" />
? ? ? ? ? ? ? ? <TextBox Name="txLocDt" Text="{Binding LocDt}" ?MinWidth="100" />
? ? ? ? ? ? </StackPanel>
? ? ? ? </WrapPanel>
? ? ? ? <WrapPanel Grid.Row="2" Orientation="Horizontal">
? ? ? ? ? ? <Button ?Name="btnRefresh" ?Content="刷新" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" ?Click="btnRefresh_Click"/>
? ? ? ? ? ? <Button ?Name="btnUpdate" Content="更新" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="btnUpdate_Click"/>
? ? ? ? </WrapPanel>
? ? </Grid>
</Window>
布局中使用了帶有 GridView 的 ListView,通過使用 GridView,您可以在 ListView 中獲取多列數(shù)據(jù),在標(biāo)記 (XAML) 中,使用 ListView.View 屬性為 ListView 定義一個(gè)視圖,在 GridView 內(nèi)部定義了4列,我們希望顯示的每條數(shù)據(jù)都對(duì)應(yīng)一列。該標(biāo)題屬性用于指定我們想顯示的列,然后我們用文字DisplayMemberBinding屬性的值綁定到從我們的CarInfo類的屬性。使用DisplayMemberBinding屬性幾乎僅限于輸出簡(jiǎn)單的字符串,根本沒有自定義格式,但 通過指定CellTemplate,我們可以完全控制內(nèi)容在特定列單元格中的呈現(xiàn)方式。
5、為刷新和更新按鈕編寫后臺(tái)代碼,具體代碼如下:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp6.model;
namespace WpfApp6.control
{
? ? /// <summary>
? ? /// ListViewWin.xaml 的交互邏輯
? ? /// </summary>
? ? public partial class ListViewWin : Window
? ? {
? ? ? ? ucdosEntities db = new ucdosEntities();//ef上下文對(duì)象
? ? ? ? public ListViewWin()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();?
? ? ? ? }
? ? ? ? ?
? ? ? ? private void GetData()
? ? ? ? {
? ? ? ? ? ? List<CarInfo> list = db.CarInfo.ToList<CarInfo>();
? ? ? ? ? ? listView1.ItemsSource = list;//綁定listview數(shù)據(jù)源
? ? ? ? }
?
? ? ? ? private void btnRefresh_Click(object sender, RoutedEventArgs e)
? ? ? ? {
? ? ? ? ? ? GetData();
? ? ? ? }
? ? ? ? private void btnUpdate_Click(object sender, RoutedEventArgs e)
? ? ? ? {
? ? ? ? ? ? int id = Convert.ToInt32(txId.Text);
? ? ? ? ? ? var objcar = db.CarInfo.Where(x=>x.ID==id).FirstOrDefault();
? ? ? ? ? ? objcar.ID = id;
? ? ? ? ? ? objcar.CarNo = txCarNo.Text;
? ? ? ? ? ? objcar.Lat = txLat.Text;
? ? ? ? ? ? objcar.Lng = txLng.Text;
? ? ? ? ? ? objcar.LocDt = DateTime.Parse(txLocDt.Text);
? ? ? ? ? ? db.SaveChanges();//ef保存
? ? ? ? ? ? MessageBox.Show("保存成功!");
? ? ? ? }
? ? }
}
這里就用到前面創(chuàng)建的EF上下文件對(duì)象類,ucdosEntities
6、運(yùn)行程序,點(diǎn)擊刷新按鈕,數(shù)據(jù)全部加載完成,上傳時(shí)間列有格式化的效果,界面如下:
?選擇某條數(shù)據(jù),下面顯示詳情信息
?這里id不能修改,其他4個(gè)可以修改,修改車牌號(hào)和緯度,點(diǎn)擊更新,效果立馬聯(lián)動(dòng),
?7、查看數(shù)據(jù)庫,數(shù)據(jù)更新了
文章來源:http://www.zghlxwxcb.cn/news/detail-442534.html
?這一波666,帥得很。文章來源地址http://www.zghlxwxcb.cn/news/detail-442534.html
到了這里,關(guān)于WPF真入門教程12--ListView控件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!