網(wǎng)站首頁 編程語言 正文
1. 前文
在普遍的也業(yè)務(wù)系統(tǒng)中, 數(shù)據(jù)要驅(qū)動(dòng)到操作的用戶界面, 它實(shí)際儲(chǔ)存的方式和表達(dá)方式會(huì)多種多樣, 數(shù)據(jù)庫存儲(chǔ)的數(shù)字 0或1, 在界面用戶看到顯示只是 成功或失敗, 或者存儲(chǔ)的字符、或更多的格式,
但是最終到界面上, 一般是需要一個(gè)轉(zhuǎn)換, 至于這個(gè)轉(zhuǎn)換是在數(shù)據(jù)庫中, 還是業(yè)務(wù)代碼中, 都是一個(gè)必不可少的操作。
2. WPF轉(zhuǎn)換器 ( IValueConverter )
WPF中, 提供一種數(shù)據(jù)轉(zhuǎn)換的接口、那就是在 System.Windows.Data 命名空間下的,IValueConverter 接口, 該接口的Convert方法可以任意的數(shù)據(jù)轉(zhuǎn)換操作。
namespace System.Windows.Data { // // 摘要: // 提供將自定義邏輯應(yīng)用于綁定的方法。 public interface IValueConverter { // // 摘要: // 轉(zhuǎn)換值。 // // 參數(shù): // value: // 綁定源生成的值。 // // targetType: // 綁定目標(biāo)屬性的類型。 // // parameter: // 要使用的轉(zhuǎn)換器參數(shù)。 // // culture: // 要用在轉(zhuǎn)換器中的區(qū)域性。 // // 返回結(jié)果: // 轉(zhuǎn)換后的值。 如果該方法返回 null,則使用有效的 null 值。 object Convert(object value, Type targetType, object parameter, CultureInfo culture); // // 摘要: // 轉(zhuǎn)換值。 // // 參數(shù): // value: // 綁定目標(biāo)生成的值。 // // targetType: // 要轉(zhuǎn)換為的類型。 // // parameter: // 要使用的轉(zhuǎn)換器參數(shù)。 // // culture: // 要用在轉(zhuǎn)換器中的區(qū)域性。 // // 返回結(jié)果: // 轉(zhuǎn)換后的值。 如果該方法返回 null,則使用有效的 null 值。 object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture); } }
3.如何使用轉(zhuǎn)換器
為了更夠簡單的描述其作用, 在后臺(tái)聲明一個(gè)int類型為 Status的變量, 然后通過綁定的形式關(guān)聯(lián)的界面層。( 如下創(chuàng)建MainViewModel層,主要用于關(guān)聯(lián)DataContext )
public class MainViewModel : ViewModelBase { /// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { } private int status; /// <summary> /// 狀態(tài) 0 / 1 /// </summary> public int Status { get { return status; } set { status = value; RaisePropertyChanged(); } } }
用戶界面綁定的Status字段, 為了能夠看到實(shí)際效果, 用了相同的字段綁定兩個(gè)進(jìn)行比較, 如下
<UniformGrid Rows="2" Columns="2" > <TextBlock Text="沒有使用轉(zhuǎn)換器的效果:" Style="{DynamicResource TextBlockStyle}" /> <TextBlock Text="{Binding Status}" Style="{DynamicResource TextBlockStyle}"/> <TextBlock Text="使用轉(zhuǎn)換器的效果:" Style="{DynamicResource TextBlockStyle}"/> <TextBlock Text="{Binding Status,Converter={StaticResource StatusConverter}}" Style="{DynamicResource TextBlockStyle}"/> </UniformGrid>
聲明一個(gè) StatusConverter 轉(zhuǎn)換器 , 改轉(zhuǎn)換器實(shí)現(xiàn)了,將數(shù)據(jù) 0 設(shè)置為未完成, 為1 則設(shè)置為完成。
public class StatusConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && int.TryParse(value.ToString(), out int result)) { if (result == 1) { return "完成"; } } return "未完成"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
界面層引用改轉(zhuǎn)換器, 完整代碼, 紅色加粗部分為引用聲明的轉(zhuǎn)換器。
<Window x:Class="WpfApp4.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:WpfApp4" xmlns:converter="clr-namespace:WpfApp4.Converter" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <converter:StatusConverter x:Key="StatusConverter"/> <Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}"> <Setter Property="TextWrapping" Value="NoWrap"/> <Setter Property="TextTrimming" Value="None"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="FontSize" Value="25"/> </Style> </Window.Resources> <Grid> <UniformGrid Rows="2" Columns="2" > <TextBlock Text="沒有使用轉(zhuǎn)換器的效果:" Style="{DynamicResource TextBlockStyle}" /> <TextBlock Text="{Binding Status}" Style="{DynamicResource TextBlockStyle}"/> <TextBlock Text="使用轉(zhuǎn)換器的效果:" Style="{DynamicResource TextBlockStyle}"/> <TextBlock Text="{Binding Status,Converter={StaticResource StatusConverter}}" Style="{DynamicResource TextBlockStyle}"/> </UniformGrid> </Grid> </Window>
測試效果 :
結(jié)尾:
WPF中, 還有一種轉(zhuǎn)換器, 同樣是位于 System.Windows.Data命名空間的IMultiValueConverter 接口, 通過ILSpy可以查看到,如下所示:
IMultiValueConverter 的作用則可能進(jìn)行多個(gè)數(shù)據(jù)源綁定, 這種騷操作稱之為, 多路綁定, 根據(jù)多個(gè)數(shù)據(jù)庫來決定最后顯示的內(nèi)容。
原文鏈接:https://www.cnblogs.com/zh7791/p/9311332.html
相關(guān)推薦
- 2023-05-12 Python?pandas?的索引方式?data.loc[],data[][]示例詳解_python
- 2022-12-30 python中的decode()與encode()深入理解_python
- 2022-09-15 Android?Jetpack庫剖析之LiveData組件篇_Android
- 2021-12-03 Android消息機(jī)制Handler深入理解_Android
- 2022-05-26 flutter實(shí)現(xiàn)底部抽屜效果_Android
- 2023-05-31 E:?無法定位軟件包?python3-pip問題及解決_python
- 2022-12-25 終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法_python
- 2022-09-10 Python學(xué)習(xí)筆記嵌套循環(huán)詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支