Silverlight 5 beta新特性探索系列:7.结合上层元素属性绑定和Style Setter上的绑定

简介:

     在Silverlight 5中添加了相对上层元素属性的绑定,还有Style Setter也可以绑定数据。

        一、相对上层元素属性的绑定

       它是在元素内部的子孙级元素中的某一些属性可以绑定为祖先级元素的某一些属性。比如说再一个ListBox的Tag元素值为:“这是第一个父级绑定”,在 ListBox.Templete下面添加一个TextBlock元素的Text属性设置为 <TextBlock Text="{Binding Tag,RelativeSource={RelativeSource  AncestorType=ListBox,AncestorLevel=1}}"/>,这样子当ListBox有数据集合的时候显示的数据行就是 值“这是第一个父级绑定”。

         下面我们来看完整的XAML源码(MainPage.xaml):


 
 
  1. <ListBox Tag="这是第一个父级绑定" Name="listBox1" Margin="100,50,169,221"> 
  2. <ListBox.ItemTemplate> 
  3. <DataTemplate> 
  4. <StackPanel> 
  5. <TextBlock Text="{Binding Tag,RelativeSource={RelativeSource 
  6. AncestorType=ListBox,AncestorLevel=1}}"/> 
  7. </StackPanel> 
  8. </DataTemplate> 
  9. </ListBox.ItemTemplate> 
  10. </ListBox> 

        接下来我们看后台代码(MainPage.xaml.cs代码):


 
 
  1. public partial class MainPage : UserControl 
  2. { 
  3. public MainPage() 
  4. { 
  5. InitializeComponent(); 
  6. //设置当前listBox1的数据源 
  7. this.listBox1.ItemsSource = new List<Person>(){ 
  8. new Person(){ Name="张三"}, 
  9. new Person(){ Name="李四"}, 
  10. new Person(){ Name="王五"}, 
  11. new Person(){ Name="刘六"} 
  12. }; 
  13. } 
  14. } 
  15. /// <summary> 
  16. /// 实体类Person 
  17. /// </summary> 
  18. public class Person 
  19. { 
  20. string _Name; 
  21.  
  22. public string Name 
  23. { 
  24. get { return _Name; } 
  25. set { _Name = value; } 
  26. } 
  27. } 

        运行程序我们可以看到效果图如左图,而非右图,

 


      

        二、Style Setter的绑定

        准备一个Style样式的源类TBTheme,此类中有多个属性,这些属性是一些样式的颜色,文字大小之类的。将此类引入到 App.xaml文件中,然后再App.xaml中的Style Setter绑定这个源类TBTheme,并且设置需要绑定的源类中的某个属性。当鼠标 单击此TextBlock的时候切换绑定的属性的值。其步骤分为四步。

             第一步:引入TBTheme类设置其key为tbTheme

             第二步:设置绑定到TBTheme类下面的TextBrush字段(App.xaml中代码如下)

 


 
 
  1. <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  3. x:Class="SL5BindInStyle.App" 
  4. xmlns:local="clr-namespace:SL5BindInStyle" 
  5. > 
  6. <Application.Resources> 
  7. <!--第一步:引入TBTheme类设置其key为tbTheme--> 
  8. <local:TBTheme x:Key="tbTheme" /> 
  9. <!--第二步:设置绑定到TBTheme类下面的TextBrush字段--> 
  10. <Style x:Key="NormalText" TargetType="TextBlock"> 
  11. <Setter Property="FontFamily" Value="Comic Sans MS" /> 
  12. <Setter Property="FontSize" Value="12" /> 
  13. <Setter Property="Foreground" Value="{Binding TextBrush, 
  14. Source={StaticResource tbTheme}}" /> 
  15. </Style> 
  16. </Application.Resources> 
  17. </Application> 

             第三步:获取到需要绑定设置的字体颜色(TBTheme.cs代码如下)


 
 
  1. using System.ComponentModel; 
  2. namespace SL5BindInStyle 
  3. { 
  4. public class TBTheme : INotifyPropertyChanged 
  5. { 
  6. //标志符,标志当前是什么颜色 
  7. bool Flag; 
  8. public TBTheme() 
  9. { 
  10. //初始化类 
  11. Flag = true; 
  12. GetOtherColor(); 
  13. } 
  14. public event PropertyChangedEventHandler PropertyChanged; 
  15. private Brush _textBrush; 
  16. /// <summary> 
  17. /// 字体颜色 
  18. /// </summary> 
  19. public Brush TextBrush 
  20. { 
  21. get { return _textBrush; } 
  22. set 
  23. { 
  24. _textBrush = value; 
  25. if (PropertyChanged != null) 
  26. PropertyChanged(this, new PropertyChangedEventArgs("TextBrush")); 
  27. } 
  28. } 
  29. /// <summary> 
  30. /// 第三步:获取其他颜色 
  31. /// </summary> 
  32. public void GetOtherColor() 
  33. { 
  34. if (Flag) 
  35. { 
  36. TextBrush = new SolidColorBrush(Colors.Blue); 
  37. Flag = false; 
  38. } 
  39. else 
  40. { 
  41. TextBrush = new SolidColorBrush(Colors.Green); 
  42. Flag = true; 
  43.  
  44. } 
  45. } 
  46. } 
  47. } 

             第四步:每次点击textBlock1就更换一次字体颜色(MainPage.xaml.cs代码)


 
 
  1. //第四步:每次点击textBlock1就更换一次字体颜色 
  2. private void textBlock1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
  3. { 
  4. (Application.Current.Resources["tbTheme"] as TBTheme).GetOtherColor(); 
  5. } 

       MainPage.xaml的主代码如下,TextBlock绑定全局静态资源App.xaml中的key为NormalText样式:


 
 
  1. <TextBlock x:Name="textBlock1" Text="可以动态改变颜色的TextBlock" 
  2. Style="{StaticResource NormalText}" Margin="100,0,169,456" 
  3. MouseLeftButtonDown="textBlock1_MouseLeftButtonDown"></TextBlock> 

        最后我们来看看点击TextBlock前后的照片如下图,如需源码请点击 SL5BindInStyle.zip 下载。

点击前效果:

   点击后效果:


本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/826413


相关文章
|
容器 前端开发 测试技术
微软Silverlight 5.0新特性
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/6088631 微软Silverlight 5.0新特性   最近微软发布了Silverlight 5.0的路线图。
934 0