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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据绑定(十)Binding的数据转换

發(fā)布時(shí)間:2025/6/16 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据绑定(十)Binding的数据转换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:數(shù)據(jù)綁定(十)Binding的數(shù)據(jù)轉(zhuǎn)換

當(dāng)Source端Path所關(guān)聯(lián)的數(shù)據(jù)與Target端目標(biāo)屬性數(shù)據(jù)類型不一致時(shí),需要添加數(shù)據(jù)轉(zhuǎn)換器,數(shù)據(jù)轉(zhuǎn)換器是一個(gè)自定義的類,這個(gè)類需要實(shí)現(xiàn)IValueConverter接口,這個(gè)接口有兩個(gè)方法需要實(shí)現(xiàn):Convert和ConvertBack,當(dāng)數(shù)據(jù)從Source流向Target時(shí),將調(diào)用Convert方法,反之,將調(diào)用ConvertBack方法

例子,首先定義飛機(jī)類型

public enum Category{Bomber,Fighter}public enum State{Available,Locked,Unknown}public class Plane{public Category Category { get; set; }public string Name { get; set; }public State State { get; set; }}
Plane類型的Category將在界面上轉(zhuǎn)換為圖片,而State類型將會(huì)轉(zhuǎn)換成界面上的CheckBox顯示,由于存在兩個(gè)轉(zhuǎn)換,因此需要提供兩個(gè)Converter,第一個(gè)轉(zhuǎn)換是做Category類型與字符類型的轉(zhuǎn)換,字符串是圖片的路徑

class CategoryToSourceConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){Category c = (Category)value;switch (c){case Category.Bomber:{return @"\Icons\close.png";}case Category.Fighter:{return @"\Icons\closeing.png";}default:{return null;}}}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){return new NotImplementedException();}}
由于UI上不能修改圖片,所以只實(shí)現(xiàn)了從Source到Target的轉(zhuǎn)換

另一個(gè)轉(zhuǎn)換用于將State數(shù)據(jù)轉(zhuǎn)換為bool?

public class StateToNullableBoolConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){State s = (State)value;switch (s){case State.Locked:{return false;}case State.Available:{return true;}case State.Unknown:default:{return null;}}}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){bool? nb = (bool?)value;switch (nb){case true:{return State.Available;}case false:{return State.Locked;}case null:default:{return State.Unknown;}}}}
界面代碼:

<Window x:Class="WpfApplication1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApplication1"Title="MainWindow" Height="275" Width="275"> <Window.Resources><local:CategoryToSourceConverter x:Key="cts" /><local:StateToNullableBoolConverter x:Key="stnb" /></Window.Resources><StackPanel Background="LightBlue"><ListBox x:Name="listBoxPlane" Height="160" Margin="5"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"> <Image Width="20" Height="20" Source="{Binding Path=Category, Converter={StaticResource cts}}" /><TextBlock Text="{Binding Path=Name}" Width="60" Margin="80,0" /><CheckBox IsThreeState="True" IsChecked="{Binding Path=State, Converter={StaticResource stnb}}" /></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox><Button x:Name="buttonLoad" Content="Load" Height="25" Margin="5,0" Click="buttonLoad_Click" /><Button x:Name="buttonSave" Content="Save" Height="25" Margin="5,0" Click="buttonSave_Click" /></StackPanel> </Window>
加粗的部分是XAML中對(duì)轉(zhuǎn)換器的使用,后臺(tái)代碼中實(shí)現(xiàn)了Load和Save兩個(gè)按鈕的點(diǎn)擊事件

private void buttonLoad_Click(object sender, RoutedEventArgs e){List<Plane> planeList = new List<Plane>(){new Plane(){Category=Category.Bomber, Name="B-1", State=State.Unknown},new Plane(){Category=Category.Bomber, Name="B-2", State=State.Unknown},new Plane(){Category=Category.Fighter, Name="F-22", State=State.Unknown},new Plane(){Category=Category.Fighter, Name="Su-47", State=State.Unknown},new Plane(){Category=Category.Bomber, Name="B-52", State=State.Unknown},new Plane(){Category=Category.Fighter, Name="J-10", State=State.Unknown}};listBoxPlane.ItemsSource = planeList;}private void buttonSave_Click(object sender, RoutedEventArgs e){StringBuilder sb = new StringBuilder();foreach (Plane p in listBoxPlane.Items){sb.AppendLine(string.Format("Category={0}, Name={1}, State={2}", p.Category, p.Name, p.State));}File.WriteAllText(@"d:\PlaneList.txt", sb.ToString());}

界面效果如圖:


當(dāng)改變checkbox的選中狀態(tài)時(shí),Plane對(duì)象中的值會(huì)發(fā)生變化


總結(jié)

以上是生活随笔為你收集整理的数据绑定(十)Binding的数据转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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