WPF入門教程系列五——Window 介紹
添加ClickAction的實(shí)現(xiàn)
????? 通過(guò)上面兩步,我們將準(zhǔn)備工具全部做完了,現(xiàn)在需要在.xmal文件中給Button按鈕的Command屬性綁定了一個(gè)方法叫做ClickSaveAction,DataGrid控件的SelectItem綁定MainWindowVM(ViewModel)中的AreaVM屬性。
1. 在Visual Studio 2022中打開MainWindows.xmal文件。
2. 對(duì)DataGrid的SelectItem進(jìn)行了數(shù)據(jù)綁定。具體代碼如下:
<DataGrid x:Name="gridArea" Grid.Row="1" d:ItemsSource="{d:SampleData ItemCount=5}"
AutoGenerateColumns="False"
HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding Path=AreaVM,
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
3.將ClickSaveCommand綁定到Button按鈕的Command屬性上,這個(gè)ClickSaveCommand指令將代替保存按鈕的click事件,將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)。具體代碼如下:
<Button x:Name="btnSave" Height="22" Width="120" Command="{Binding ClickSaveAction}" >保存</Button>
注意:Command屬性僅僅作為Click行為的綁定,其他行為,如鼠標(biāo)移入、移出等事件,要使用另外的MVVM方式進(jìn)行綁定。
4.MainWindow.xmal的全部代碼如下:
<Window x:Class="WpfGridDemo.NET7.MainWindow"
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:WpfGridDemo.NET7"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="960">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid x:Name="gridArea" Grid.Row="1" d:ItemsSource="{d:SampleData ItemCount=5}"
AutoGenerateColumns="False"
HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding Path=AreaVM,
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="城市" Width="120" x:Name="cboCity"
ClipboardContentBinding="{x:Null}" SelectedValuePath="Code"
SelectedValueBinding="{Binding Path=CityCode,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name" SelectedItemBinding="{x:Null}" />
<DataGridTextColumn Header="縣區(qū)鎮(zhèn)" Width="*" Binding="{Binding Name}"
ClipboardContentBinding="{x:Null}"/>
<DataGridTextColumn Header="郵編" Width="100" Binding="{Binding Code}"
ClipboardContentBinding="{x:Null}"/>
<DataGridTextColumn Header="創(chuàng)建時(shí)間" Width="160" Binding="{Binding Created}"
ClipboardContentBinding="{x:Null}"/>
<DataGridTextColumn Header="更新時(shí)間" Width="160" Binding="{Binding Updated}"
ClipboardContentBinding="{x:Null}"/>
</DataGrid.Columns>
</DataGrid>
<WrapPanel Grid.Row="2">
<Button x:Name="btnRefresh" Height="22" Width="120" Click="btnRefresh_Click">刷新</Button>
<Button x:Name="btnSave" Height="22" Width="120" Command="{Binding ClickSaveAction}" >保存</Button>
</WrapPanel>
</Grid>
</Window>
5.在Visual Studio 2022中按F5鍵,啟動(dòng)WPF應(yīng)用程序,使用鼠標(biāo)左鍵點(diǎn)擊“刷新”按鈕,在數(shù)據(jù)呈現(xiàn)之后,使用鼠標(biāo)左鍵選中DataGrid中的一條記錄,進(jìn)行修改,然后點(diǎn)擊“保存”按鈕。如下圖。
6. 使用鼠標(biāo)左鍵點(diǎn)擊“刷新”按鈕,在數(shù)據(jù)呈現(xiàn)之后,我們發(fā)現(xiàn),剛才所做的修改,已經(jīng)保存到數(shù)據(jù)庫(kù)了。如下圖。?
數(shù)據(jù)已經(jīng)保存到數(shù)據(jù)庫(kù),如下圖。
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-478704.html
7.如果我們要讓保存按鈕禁用,可以將執(zhí)行的方法返回為False,具體代碼如下:
/// <summary>
/// 命令是否可以執(zhí)行
/// </summary>
/// <returns></returns>
bool CanSaveExecute()
{
return false;
}
8. 在Visual Studio 2022中按F5鍵,啟動(dòng)WPF應(yīng)用程序,能夠看到,界面中按鈕已經(jīng)是禁用狀態(tài)了,我們綁定的這個(gè)命令是否可以執(zhí)行,是直接影響到按鈕能否被點(diǎn)擊的!這個(gè)值會(huì)直接作用在按鈕的IsEnabled上。如下圖。
?
?
七、ComboBox下拉事件轉(zhuǎn)為Command命令
用于 DataGridComboBoxColumn 顯示有一組項(xiàng)可供選擇的數(shù)據(jù),例如枚舉。 DataGridComboBoxColumn 允許用戶從下拉列表中選擇項(xiàng)。本文通過(guò)將Command命令綁定到comboBox的SelectionChanged事件上,實(shí)現(xiàn)自動(dòng)刷新用戶選擇的省份的相應(yīng)城市信息。
在WPF中默認(rèn)的Command綁定往往不能滿足覆蓋所有的事件,例如ComboBox的SelectionChanged事件,DataGrid的SelectionChanged事件等等,這時(shí)就可以用到一個(gè)擴(kuò)展庫(kù),來(lái)實(shí)現(xiàn)事件綁定Command。
微軟從WPF 4.0開始,引入了一個(gè)比較實(shí)用的庫(kù)——Interactions,這個(gè)庫(kù)主要是通過(guò)附加屬性來(lái)對(duì)UI控件注入一些新的功能,除了內(nèi)置了一系列比較好用的功能外,還提供了比較良好的擴(kuò)展接口。
??? 本文這里簡(jiǎn)單的介紹一下Behavior這個(gè)擴(kuò)展,顧名思義,Behavior可以賦予控件新的行為能力。
??? 事件綁定到Command需要用到Interaction.Triggers這個(gè)屬性 ,在這個(gè)屬性里面添加一個(gè)或多個(gè)EventTrigger并指定關(guān)注的的事件名稱,在EventTrigger中通過(guò)InvokeCommandAction來(lái)綁定事件對(duì)應(yīng)的command。
1. 在Visual Studio 2022中打開MainWindows.xmal文件,并在文件的開頭添加如下命名空間。
xmlns:be="http://schemas.microsoft.com/xaml/behaviors"
2. 在MainWindows.xmal文件中添加一個(gè)ComboBox控件,具體代碼如下:
<WrapPanel Grid.Row="0" HorizontalAlignment="Left">
<ComboBox x:Name="cboProvince" DisplayMemberPath="Name" SelectedValuePath="Code" >
<be:Interaction.Triggers>
<be:EventTrigger EventName="SelectionChanged">
<be:InvokeCommandAction Command="{Binding ProviceChangedAction}"/>
</be:EventTrigger>
</be:Interaction.Triggers>
</ComboBox>
</WrapPanel>
3.???? 在寫完了以上代碼之后,Visual Studio 2022的界面設(shè)計(jì)器中原來(lái)呈現(xiàn)的UI界面,消失了,顯示“無(wú)效標(biāo)記,有關(guān)詳細(xì)信息,請(qǐng)查看錯(cuò)誤列表”。如下圖。
4.從錯(cuò)誤信息中來(lái)查看,應(yīng)用程序缺少程序集,沒(méi)有安裝相應(yīng)的程序包。使用鼠標(biāo)在菜單欄中選擇“工具--》NuGet軟件包管理器- -》 管理此解決方案的NuGet程序包”,如下圖。
5.??? 在Visual Studio 2022的NuGet-解決方案標(biāo)簽頁(yè)中,瀏覽頁(yè)面的搜索框中輸入“Microsoft.Xaml.Behaviors.Wpf”進(jìn)行搜索,然后使用鼠標(biāo)左鍵先選中要安裝的項(xiàng)目名稱,然后再點(diǎn)擊“安裝”按鈕。如下圖。
6.安裝成功之后,錯(cuò)誤列表中的錯(cuò)誤信息消失了,UI設(shè)計(jì)器中的UI又回來(lái)了。如下圖。
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-478704.html
?
到了這里,關(guān)于WPF入門教程系列二十八 ——DataGrid使用示例MVVM模式(5)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!