日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF使用转换器(Converter)

發(fā)布時間:2024/3/7 asp.net 87 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF使用转换器(Converter) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.作用:可以將源數(shù)據(jù)和目標(biāo)數(shù)據(jù)之間進行特定的轉(zhuǎn)化,

2.定義轉(zhuǎn)換器,需要繼承接口IValueConverter

[ValueConversion(typeof(int), typeof(string))] public class ForeColorConverter : IValueConverter {//源屬性傳給目標(biāo)屬性時,調(diào)用此方法ConvertBackpublic object Convert(object value, Type targetType, object parameter, CultureInfo culture){int c = System.Convert.ToInt32(parameter);if (value == null)throw new ArgumentNullException("value can not be null");int index = System.Convert.ToInt32(value);if (index == 0)return "Blue";else if (index == 1)return "Red";elsereturn "Green";}//目標(biāo)屬性傳給源屬性時,調(diào)用此方法ConvertBackpublic object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){return null;} } 

public ValueConversionAttribute(Type sourceType, TypetargetType):指定源屬性類型和目標(biāo)屬性類型

Convert:會進行源屬性傳給目標(biāo)屬性的特定轉(zhuǎn)化

ConvertBack:會進行目標(biāo)屬性傳給源屬性的特定轉(zhuǎn)化

加粗樣式參數(shù)parameter:對應(yīng)Binding的ConverterParameter屬性

3.使用轉(zhuǎn)換器

(1)引用轉(zhuǎn)換器所在的命名空間

xmlns:local1="clr-namespace:WpfTest.View"

(2)定義資源

<Window.Resources><local1:ForeColorConverter x:Key="foreColor"></local1:ForeColorConverter> </Window.Resources>

(3)定義屬性

private int status = 0; public int Status {get => status; set { status = value; RaisePropertyChanged("Status"); }}

(4)綁定屬性,添加轉(zhuǎn)換器

<Grid><Label HorizontalAlignment="Left" Height="23" Margin="243,208,0,0" Content="這里哦" Foreground="{Binding Status,Converter={StaticResource foreColor},Mode=OneWay}" VerticalAlignment="Top" Width="120"/><TextBox x:Name="tbName" HorizontalAlignment="Left" Height="23" Margin="243,160,0,0" TextWrapping="Wrap" Text="{Binding Status,UpdateSourceTrigger=LostFocus,Mode=OneWayToSource}" VerticalAlignment="Top" Width="120"/><Button Content="Button" HorizontalAlignment="Left" Margin="389,160,0,0" VerticalAlignment="Top" Width="75"/> </Grid>

4.效果


其它示例:
1、定義轉(zhuǎn)換器,需要繼承接口IValueConverter

public class LongToBoolConverter : System.Windows.Data.IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value == null){return new NotImplementedException();}double vv = (string)value == "" ? 0 : double.Parse((string)value);return vv > 8 ? true : false;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}

2、定義資源

<bandit:BanditWindow.Resources><localHelper:LongToBoolConverter x:Key="LongToBoolConverter"/> </bandit:BanditWindow.Resources>

3、數(shù)據(jù)綁定

<TextBox Width="200" Height="50" Name="txtBoxName" VerticalContentAlignment="Center"> <TextBox.Style><Style TargetType="TextBox"><Style.Triggers><DataTrigger Binding="{Binding ElementName=txtBoxName,Path=Text,Converter={StaticResource LongToBoolConverter}}" Value="True" ><Setter Property="Background" Value="Green"></Setter></DataTrigger><DataTrigger Binding="{Binding ElementName=txtBoxName,Path=Text,Converter={StaticResource LongToBoolConverter}}" Value="False" ><Setter Property="Background" Value="Red"></Setter></DataTrigger></Style.Triggers></Style></TextBox.Style></TextBox>

4、效果展示:

總結(jié)

以上是生活随笔為你收集整理的WPF使用转换器(Converter)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。