原創(chuàng)|其它|編輯:郝浩|2013-01-05 09:52:19.000|閱讀 528 次
概述:在WPF Elements數(shù)據(jù)網(wǎng)格使用筆記(二)中已經(jīng)構(gòu)建了一個(gè)簡(jiǎn)單示例,今天將添加一個(gè)自定義模版到該示例中,以便在余額列中以貨幣的形式顯示數(shù)據(jù),可以通過設(shè)置DataGridColumn的DisplayTemplate屬來實(shí)現(xiàn)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Mindscape WPF Elements中有很多強(qiáng)大的功能特性,比如說單元格模版的應(yīng)用,自定義編輯器,以及數(shù)據(jù)驗(yàn)證等,這些功能都非常的易于使用。在幾分鐘的時(shí)間內(nèi),你就可以構(gòu)建運(yùn)行一個(gè)數(shù)據(jù)網(wǎng)格,還可以按照用戶指定的風(fēng)格方式呈現(xiàn)數(shù)據(jù),還會(huì)在編輯的模式下提供提示,或是使用想要的驗(yàn)證碼檢查數(shù)據(jù)是否是正確。
在前面的文中,我們已經(jīng)構(gòu)建好了一個(gè)簡(jiǎn)單的示例,在今天的文中將會(huì)添加一個(gè)自定義模版到前面的示例中去,以便在余額列中以貨幣的形式顯示數(shù)據(jù),這個(gè)就可以通過設(shè)置DataGridColumn的DisplayTemplate屬來實(shí)現(xiàn)。Mindscape WPF Elements中有很多強(qiáng)大的控件用于數(shù)據(jù)的顯示和編輯,這個(gè)CurrencyTextBox就可以很好的實(shí)現(xiàn)剛才的想法,它可以極好的自動(dòng)格式化信息屬性的值到當(dāng)前區(qū)域的貨幣信息,能夠精確到小數(shù)點(diǎn)和美分(在有的情況下),CurrencyTextBox會(huì)將其作為一種資源進(jìn)行添加:
<!-- style information to make it look how we want --> <Style x:Key="NegativeBalanceStyle" TargetType="TextBox"> <Setter Property="Background" Value="Transparent" /> <Setter Property="Foreground" Value="DarkRed" /> <Setter Property="BorderThickness" Value="0" /> </Style> <!-- add the currency text box to our data template --> <DataTemplate x:Key="BalanceCellTemplate"> <ms:CurrencyTextBox Value="{Binding Balance}" NegativeStyle="{StaticResource NegativeBalanceStyle}" Background="Transparent" Foreground="White" /> </DataTemplate>
并將其分配到的屬性:
<ms:DataGridColumn PropertyName="Balance" Width="140" DisplayTemplate="{StaticResource BalanceCellTemplate}"/>
將會(huì)格式化余額列:
對(duì)于電話號(hào)碼列將會(huì)有一個(gè)文本輸入框來幫助用戶輸入數(shù)字,可以用下面的方式來應(yīng)用我們的特別的文本框到你的電話號(hào)碼列:
<DataTemplate x:Key="PhoneEditor"> <ms:MaskedTextBox Mask="00-000-0000" AutoSkipLiterals="True" Text="{Binding Phone}" LiteralStyle="{StaticResource Literal}" PromptStyle="{StaticResource Prompt}" Background="Transparent" PromptCharDisplaySelector="{StaticResource PromptCharDisplaySelector}" /> </DataTemplate> <Style x:Key="Literal" TargetType="Inline"> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="Foreground" Value="DarkGoldenrod" /> </Style> <Style x:Key="Prompt" TargetType="Run"> <Setter Property="Foreground" Value="LightGray" /> <Setter Property="FontFamily" Value="Wingdings" /> </Style> <local:FancyPromptCharDisplaySelector x:Key="PromptCharDisplaySelector" />
這時(shí)再設(shè)置屬性:
<ms:DataGridColumn PropertyName="Phone" Width="120" EditorTemplate="{StaticResource PhoneEditor}" />
下面來看一下驗(yàn)證的問題,將會(huì)提供一個(gè)ValidateCell事件,當(dāng)數(shù)據(jù)發(fā)生更改時(shí),就會(huì)出現(xiàn),具體實(shí)現(xiàn)方式如下所示:
object value = e.Cell.Value; switch (e.Cell.Column.PropertyName) { case "Phone": string number = value as string; if (number.Length != 11) { e.IsValid = false; e.ValidationMessage = "Number must contain 9 numerals"; } break; case "Address": string address = value as string; if (address.Length == 0) { e.IsValid = false; e.ValidationMessage = "Address cannot be null"; } break; }
上面的代碼將會(huì)出先下面的效果:
當(dāng)在電話號(hào)碼列中進(jìn)行數(shù)據(jù)的編輯時(shí),系統(tǒng)就會(huì)進(jìn)行提示,如果是收到的無效的信息單元格就會(huì)顯示一個(gè)紅色的邊框。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件