原創(chuàng)|其它|編輯:郝浩|2013-01-05 09:52:19.000|閱讀 528 次
概述:在WPF Elements數(shù)據(jù)網(wǎng)格使用筆記(二)中已經(jīng)構(gòu)建了一個簡單示例,今天將添加一個自定義模版到該示例中,以便在余額列中以貨幣的形式顯示數(shù)據(jù),可以通過設(shè)置DataGridColumn的DisplayTemplate屬來實現(xiàn)。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Mindscape WPF Elements中有很多強大的功能特性,比如說單元格模版的應(yīng)用,自定義編輯器,以及數(shù)據(jù)驗證等,這些功能都非常的易于使用。在幾分鐘的時間內(nèi),你就可以構(gòu)建運行一個數(shù)據(jù)網(wǎng)格,還可以按照用戶指定的風(fēng)格方式呈現(xiàn)數(shù)據(jù),還會在編輯的模式下提供提示,或是使用想要的驗證碼檢查數(shù)據(jù)是否是正確。
在前面的文中,我們已經(jīng)構(gòu)建好了一個簡單的示例,在今天的文中將會添加一個自定義模版到前面的示例中去,以便在余額列中以貨幣的形式顯示數(shù)據(jù),這個就可以通過設(shè)置DataGridColumn的DisplayTemplate屬來實現(xiàn)。Mindscape WPF Elements中有很多強大的控件用于數(shù)據(jù)的顯示和編輯,這個CurrencyTextBox就可以很好的實現(xiàn)剛才的想法,它可以極好的自動格式化信息屬性的值到當(dāng)前區(qū)域的貨幣信息,能夠精確到小數(shù)點和美分(在有的情況下),CurrencyTextBox會將其作為一種資源進行添加:
<!-- 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}"/>
將會格式化余額列:
對于電話號碼列將會有一個文本輸入框來幫助用戶輸入數(shù)字,可以用下面的方式來應(yīng)用我們的特別的文本框到你的電話號碼列:
<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è)置屬性:
<ms:DataGridColumn PropertyName="Phone" Width="120" EditorTemplate="{StaticResource PhoneEditor}" />
下面來看一下驗證的問題,將會提供一個ValidateCell事件,當(dāng)數(shù)據(jù)發(fā)生更改時,就會出現(xiàn),具體實現(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; }
上面的代碼將會出先下面的效果:
當(dāng)在電話號碼列中進行數(shù)據(jù)的編輯時,系統(tǒng)就會進行提示,如果是收到的無效的信息單元格就會顯示一個紅色的邊框。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件