WP 数据绑定Visibility
发现又懒了很久了
随便画点字吧
在WP中,一些控件(如ListBox)模板数据绑定的时候,可能需要根据需要绑定的数据,来动态的选择一些内容是否显示(额,不会表达,其实简单的说就是绑定控件的Visibility)
其实方法是和SL一样的,需要一个转换类来转换成Visibility.Collapsed或者Visibility.Visible
比如新建一个工程VisibilityBinding
然后创建一个转换类ConvertVisibility.cs,继承于IValueConverter(记得添加引用using System.Windows.Data)
代码:
1 using System;
2 using System.Windows;
3 using System.Windows.Data;
4
5 namespace VisibilityBinding
6 {
7 public class ConvertVisibility : IValueConverter
8 {
9 public ConvertVisibility()
10 {
11 }
12
13 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
14 {
15 if (value == null)
16 {
17 //设置当输入不存在时的默认返回值
18 return Visibility.Collapsed;
19 }
20 bool s = (bool)value;
21 //根据输入,判断返回值
22 return (s != true) ? Visibility.Collapsed : Visibility.Visible;
23 }
24 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
25 {
26 throw new NotImplementedException();
27 }
28 }
29 }
2 using System.Windows;
3 using System.Windows.Data;
4
5 namespace VisibilityBinding
6 {
7 public class ConvertVisibility : IValueConverter
8 {
9 public ConvertVisibility()
10 {
11 }
12
13 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
14 {
15 if (value == null)
16 {
17 //设置当输入不存在时的默认返回值
18 return Visibility.Collapsed;
19 }
20 bool s = (bool)value;
21 //根据输入,判断返回值
22 return (s != true) ? Visibility.Collapsed : Visibility.Visible;
23 }
24 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
25 {
26 throw new NotImplementedException();
27 }
28 }
29 }
我们再添加一个数据类itemdata,用于绑定数据:
1 namespace VisibilityBinding
2 {
3 public class itemdata
4 {
5 public string Name
6 { get; set; }
7 public string Score
8 { get; set; }
9 public bool Visibilitytype
10 { get; set; }
11 }
12 }
2 {
3 public class itemdata
4 {
5 public string Name
6 { get; set; }
7 public string Score
8 { get; set; }
9 public bool Visibilitytype
10 { get; set; }
11 }
12 }
然后我们回到MainPage.xaml,拖入个Listbox,并简单编辑下绑定模板
然后现在页面顶部添加引用
1 xmlns:Local="clr-namespace:VisibilityBinding"
然后在页面里添加资源:
1 <Grid.Resources>
2 <Local:ConvertVisibility x:Key="dataconverter"/>
3 </Grid.Resources>
2 <Local:ConvertVisibility x:Key="dataconverter"/>
3 </Grid.Resources>
最后设定绑定数据的转换:
1 <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Margin="0,0,15,0" Width="200" Visibility="{Binding Path=Visibilitytype,Converter={StaticResource dataconverter}}" />
然后设定数据绑定:
1 using System.Collections.Generic;
2 using System.Windows;
3 using Microsoft.Phone.Controls;
4
5 namespace VisibilityBinding
6 {
7 public partial class MainPage : PhoneApplicationPage
8 {
9 public MainPage()
10 {
11 InitializeComponent();
12 }
13
14 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
15 {
16 List<itemdata> idata = new List<itemdata>();
17 idata.Add(new itemdata("语文","80",true));
18 idata.Add(new itemdata("数学", "86", false));
19 idata.Add(new itemdata("英语", "84", true));
20 idata.Add(new itemdata("计算机", "76", false));
21 lb.ItemsSource = idata;
22 }
23 }
24 }
2 using System.Windows;
3 using Microsoft.Phone.Controls;
4
5 namespace VisibilityBinding
6 {
7 public partial class MainPage : PhoneApplicationPage
8 {
9 public MainPage()
10 {
11 InitializeComponent();
12 }
13
14 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
15 {
16 List<itemdata> idata = new List<itemdata>();
17 idata.Add(new itemdata("语文","80",true));
18 idata.Add(new itemdata("数学", "86", false));
19 idata.Add(new itemdata("英语", "84", true));
20 idata.Add(new itemdata("计算机", "76", false));
21 lb.ItemsSource = idata;
22 }
23 }
24 }
运行看看效果:
其实真个很简单,关键是在转换类做对应的处理,即使输入不是bool型的,只要在转换类做对应的处理,返回Visibility.Collapsed或者Visibility.Visible就可以了
源码:
作者:sun8134
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。