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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

silverlight 使用IValueConverter 转换

發布時間:2024/8/23 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 silverlight 使用IValueConverter 转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

在綁定數據中 有時候我們需要轉換相關數據類型 silverlight提供了一個System.Windows.Data.IValueConverter接口
它提供兩個方法 Convert和ConvertBack??前者是資源到目標元素時轉換??后者是目標到資源時轉換?
先創建一個類型?
? ? public class DataTimeConvert : System.Windows.Data.IValueConverter
? ? {
? ?? ???// 參數:
? ?? ???//? ?value:
? ?? ???//? ???正傳遞到源的目標數據。
? ?? ???//
? ?? ???//? ?targetType:
? ?? ???//? ???源對象需要的數據的 System.Type。
? ?? ???//
? ?? ???//? ?parameter:
? ?? ???//? ???要在轉換器邏輯中使用的可選參數。
? ?? ???//
? ?? ???//? ?culture:
? ?? ???//? ???轉換的區域性。
? ?? ???//
? ?? ???public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
? ?? ???{
? ?? ?? ?? ?return??(value).ToString();

    

//string _value = value.ToString();
//if (_value != null)
//{
// //MessageBox.Show(_value + " if");
// return ((_value).Equals("1") ? "啟用" : "停用");
//}
//else
//{
// //MessageBox.Show(value + " else");
// return 33;
//}

?

?


? ?? ???}
? ?? ???// 參數:
? ?? ???//? ?value:
? ?? ???//? ???正傳遞到源的目標數據。
? ?? ???//
? ?? ???//? ?targetType:
? ?? ???//? ???源對象需要的數據的 System.Type。
? ?? ???//
? ?? ???//? ?parameter:
? ?? ???//? ???要在轉換器邏輯中使用的可選參數。
? ?? ???//
? ?? ???//? ?culture:
? ?? ???//? ???轉換的區域性。
? ?? ???//
? ?? ???public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
? ?? ???{

//if (targetType != typeof(int)) throw new InvalidOperationException("轉換的值必須是字符!");
//return (value.ToString() == "啟用" ? 1 : 0);


? ?? ?? ?? ?return value.ToString();
? ?? ???}
然后xaml代碼?
<UserControl x:Class="SilverlightApplication1.convertControl"
? ? 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"
? ? mc:Ignorable="d"
? ? d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:my="clr-namespace:SilverlightApplication1">
? ? <UserControl.Resources>
? ?? ???<my:DataTimeConvert x:Key="DataTimeConvert1" /> //這是轉換實例化
? ? </UserControl.Resources>
? ? <Grid x:Name="LayoutRoot" Background="White">
? ?? ???<TextBox Height="23" HorizontalAlignment="Left" Margin="32,23,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
? ?? ? <TextBox Height="23" HorizontalAlignment="Left" Margin="170,23,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=Text, Mode=TwoWay, ElementName=textBox1,?Converter={StaticResource DataTimeConvert1}}" />
? ? </Grid>
</UserControl>

請用vs 在轉換類型中兩個方法設置斷點 在瀏覽器測試輸入文字進行觀察

轉載于:https://www.cnblogs.com/meimao5211/p/3464926.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的silverlight 使用IValueConverter 转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。