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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows Phone MultiBinding :Cimbalino Toolkit

發布時間:2024/4/13 windows 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows Phone MultiBinding :Cimbalino Toolkit 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在WPF和WIN8中是支持MultiBinding

?

這個有啥用呢,引用下MSDN的例子http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx:

MultiBinding allows you to bind a binding target property to a list of source properties and then apply logic to produce a value with the given inputs. This example demonstrates how to use MultiBinding.

In the following example, NameListData refers to a collection of PersonName objects, which are objects that contain two properties, firstName and lastName. The following example produces a TextBlock that shows the first and last names of a person with the last name first.

<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}"> <TextBlock.Text> <MultiBinding Converter="{StaticResource myNameConverter}" ConverterParameter="FormatLastFirst"> <Binding Path="FirstName"/> <Binding Path="LastName"/> </MultiBinding> </TextBlock.Text> </TextBlock> public class NameConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { string name; ? switch ((string)parameter) { case "FormatLastFirst": name = values[1] + ", " + values[0]; break; case "FormatNormal": default: name = values[0] + " " + values[1]; break; } ? return name; } ? public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { string[] splitValues = ((string)value).Split(' '); return splitValues; } }

?

可惜在wp中這個被砍掉了

想要 MultiBinding 需要自己來實現(方法見高手博客:http://www.devdiv.com/wp_amp_win_-blog-55433-51080.html)

當然也有一個稍微簡單點的方法,使用Cimbalino Windows Phone Toolkit(http://cimbalino.org/),Cimbalino Toolkit 的作者為我們提供了一個MultiBindingBehavior,可以比較方便的實現 MultiBinding

?

下面看一個實例把,這里我們模擬一個顯示學生各科成績的應用,在應用中有一個ListPicker,應用會根據ListPicker的值來作為閾值,對成績的顏色進行修改,下面是實現:

?

我們先新建一個wp工程(7.1,8.0都行,Cimbalino toolkit is compatible with the Windows Phone SDK 7.1.x and Windows Phone 8.0,這里我用7.1的,主要是機器不行,8.0的模擬器太卡…)

?

添加成績類:

namespace PhoneIValueConverterMultibinding1 { public class Score { public string name { get; set; } public int number { get; set; } } }

在工程中添加Cimbalino Windows Phone Toolkit

?

然后定義我們的顏色轉換類(由于我沒用雙向綁定,所以沒寫ConvertBack…):

using System; using System.Windows.Media; using System.Globalization; using Cimbalino.Phone.Toolkit.Converters; namespace PhoneIValueConverterMultibinding1 { public class ColorChange : MultiValueConverterBase { public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values.Length < 2) { return new SolidColorBrush(Colors.White); } ? if ((values[0] != null) && (values[1] != null)) { //根據輸入,判斷返回值 try { int limitnumber = System.Convert.ToInt32((string)values[0]); int studentscore = (int)values[1]; ? if (limitnumber < studentscore) { return new SolidColorBrush(Colors.Red); } else { return new SolidColorBrush(Colors.Green); } } catch (FormatException) { return new SolidColorBrush(Colors.White); } catch (Exception) { return new SolidColorBrush(Colors.White); } } else { return new SolidColorBrush(Colors.White); } } ? public override object[] ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }

?

然后在xaml里添加引用:

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:Local="clr-namespace:PhoneIValueConverterMultibinding1" xmlns:cimbalinoBehaviors="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"

?

添加ListPicker

<toolkit:ListPicker Header="成績閾值" Name="listpicker1" SelectionChanged="listpicker1_SelectionChanged"> <toolkit:ListPickerItem Content="50" /> <toolkit:ListPickerItem Content="60" /> <toolkit:ListPickerItem Content="70" /> <toolkit:ListPickerItem Content="80" /> <toolkit:ListPickerItem Content="90" /> </toolkit:ListPicker>

?

添加頁面資源

<phone:PhoneApplicationPage.Resources><Local:ColorChange x:Key="ColorConverter" /></phone:PhoneApplicationPage.Resources>

?

然后添加綁定的listbox,并在其中添加MultiBindingBehavior ,因為我們要改變的是文字的顏色,所以設置PropertyName="Foreground"

<ListBox Height="510" Name="lb1" Margin="12,12,0,0" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Width="432" Orientation="Horizontal"> <TextBlock TextWrapping="Wrap" Text="{Binding name}" FontSize="32" Width="200" /> <TextBlock TextWrapping="Wrap" Text="{Binding number}" FontSize="32" Margin="0" > <i:Interaction.Behaviors> <cimbalinoBehaviors:MultiBindingBehavior Converter="{StaticResource ColorConverter}" PropertyName="Foreground" > <cimbalinoBehaviors:MultiBindingItem Value="{Binding ElementName=listpicker1, Path=SelectedItem.Content}" /> <cimbalinoBehaviors:MultiBindingItem Value="{Binding number}" /> </cimbalinoBehaviors:MultiBindingBehavior> </i:Interaction.Behaviors> </TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

?

最后數據綁定:

ObservableCollection<Score> StudentScore = new ObservableCollection<Score>(); ? private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { StudentScore.Add(new Score { name = "語文", number = 55 }); StudentScore.Add(new Score { name = "數學", number = 66 }); StudentScore.Add(new Score { name = "英語", number = 76 }); StudentScore.Add(new Score { name = "物理", number = 72 }); StudentScore.Add(new Score { name = "化學", number = 97 }); StudentScore.Add(new Score { name = "生物", number = 86 }); StudentScore.Add(new Score { name = "地理", number = 68 }); StudentScore.Add(new Score { name = "歷史", number = 95 }); StudentScore.Add(new Score { name = "政治", number = 39 }); ? } ? private void listpicker1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (listpicker1 != null) { if (listpicker1.SelectedIndex != -1) { lb1.ItemsSource = StudentScore; } } }

?

看看效果:

?

最后說下Ms強烈建議不要復雜綁定中是使用Converter

?

另外Cimbalino Windows Phone Toolkit還有許多其他的功能

大家可以試試

?

?

源碼:

?

?

?

參考:

http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

https://github.com/Cimbalino/Cimbalino-Phone-Toolkit

http://www.pedrolamas.com/2013/05/17/cimbalino-windows-phone-toolkit-multibindingbehavior/

總結

以上是生活随笔為你收集整理的Windows Phone MultiBinding :Cimbalino Toolkit的全部內容,希望文章能夠幫你解決所遇到的問題。

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