WPF轉換器IValueConverter用法
1. 前文
在普遍的也業(yè)務系統(tǒng)中, 數(shù)據(jù)要驅動到操作的用戶界面, 它實際儲存的方式和表達方式會多種多樣, 數(shù)據(jù)庫存儲的數(shù)字 0或1, 在界面用戶看到顯示只是 成功或失敗, 或者存儲的字符、或更多的格式,
但是最終到界面上, 一般是需要一個轉換, 至于這個轉換是在數(shù)據(jù)庫中, 還是業(yè)務代碼中, 都是一個必不可少的操作。
2. WPF轉換器 ( IValueConverter )
WPF中, 提供一種數(shù)據(jù)轉換的接口、那就是在 System.Windows.Data 命名空間下的,IValueConverter 接口, 該接口的Convert方法可以任意的數(shù)據(jù)轉換操作。
namespace System.Windows.Data
{
//
// 摘要:
// 提供將自定義邏輯應用于綁定的方法。
public interface IValueConverter
{
//
// 摘要:
// 轉換值。
//
// 參數(shù):
// value:
// 綁定源生成的值。
//
// targetType:
// 綁定目標屬性的類型。
//
// parameter:
// 要使用的轉換器參數(shù)。
//
// culture:
// 要用在轉換器中的區(qū)域性。
//
// 返回結果:
// 轉換后的值。 如果該方法返回 null,則使用有效的 null 值。
object Convert(object value, Type targetType, object parameter, CultureInfo culture);
//
// 摘要:
// 轉換值。
//
// 參數(shù):
// value:
// 綁定目標生成的值。
//
// targetType:
// 要轉換為的類型。
//
// parameter:
// 要使用的轉換器參數(shù)。
//
// culture:
// 要用在轉換器中的區(qū)域性。
//
// 返回結果:
// 轉換后的值。 如果該方法返回 null,則使用有效的 null 值。
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}
}3.如何使用轉換器
為了更夠簡單的描述其作用, 在后臺聲明一個int類型為 Status的變量, 然后通過綁定的形式關聯(lián)的界面層。( 如下創(chuàng)建MainViewModel層,主要用于關聯(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字段, 為了能夠看到實際效果, 用了相同的字段綁定兩個進行比較, 如下
<UniformGrid Rows="2" Columns="2" >
<TextBlock Text="沒有使用轉換器的效果:" Style="{DynamicResource TextBlockStyle}" />
<TextBlock Text="{Binding Status}" Style="{DynamicResource TextBlockStyle}"/>
<TextBlock Text="使用轉換器的效果:" Style="{DynamicResource TextBlockStyle}"/>
<TextBlock Text="{Binding Status,Converter={StaticResource StatusConverter}}" Style="{DynamicResource TextBlockStyle}"/>
</UniformGrid>聲明一個 StatusConverter 轉換器 , 改轉換器實現(xiàn)了,將數(shù)據(jù) 0 設置為未完成, 為1 則設置為完成。
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();
}
}界面層引用改轉換器, 完整代碼, 紅色加粗部分為引用聲明的轉換器。
<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="沒有使用轉換器的效果:" Style="{DynamicResource TextBlockStyle}" />
<TextBlock Text="{Binding Status}" Style="{DynamicResource TextBlockStyle}"/>
<TextBlock Text="使用轉換器的效果:" Style="{DynamicResource TextBlockStyle}"/>
<TextBlock Text="{Binding Status,Converter={StaticResource StatusConverter}}" Style="{DynamicResource TextBlockStyle}"/>
</UniformGrid>
</Grid>
</Window>測試效果 :

結尾:
WPF中, 還有一種轉換器, 同樣是位于 System.Windows.Data命名空間的IMultiValueConverter 接口, 通過ILSpy可以查看到,如下所示:

IMultiValueConverter 的作用則可能進行多個數(shù)據(jù)源綁定, 這種騷操作稱之為, 多路綁定, 根據(jù)多個數(shù)據(jù)庫來決定最后顯示的內容。
到此這篇關于WPF轉換器IValueConverter用法的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在.NET中掃描局域網(wǎng)服務的實現(xiàn)方法
下面小編就為大家分享一篇在.NET中掃描局域網(wǎng)服務的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
ASP.NET MVC5網(wǎng)站開發(fā)之實現(xiàn)數(shù)據(jù)存儲層功能(三)
這篇文章主要為大家詳細介紹了ASP.NET MVC5網(wǎng)站開發(fā)之實現(xiàn)數(shù)據(jù)存儲層功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
在 ASP.Net Core 中使用 MiniProfiler的方法
這篇文章主要介紹了在 ASP.Net Core 中使用 MiniProfiler的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
.NET Windbg分析某婦產(chǎn)醫(yī)院WPF內存溢出
這篇文章主要為大家介紹了.NET Windbg分析某婦產(chǎn)醫(yī)院WPF內存溢出,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
ASP.NET Core開發(fā)教程之Logging利用NLog寫日志文件
一直很喜歡 NLog 的簡潔和擴展性,所以下面這篇文章主要給大家介紹了關于ASP.NET Core開發(fā)教程之Logging利用NLog寫日志文件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07

