关于Silverlight对匿名类型数据绑定的问题及其解决方法
来源:互联网 发布:西安java讲师 编辑:程序博客网 时间:2024/04/30 12:15
这是一个不得不说一下的问题。同样的代码在WPF应用程序中是可以工作的,而在Silverlight中却不可以。我们来看一下例子吧
1.WPF窗体标记
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ItemsControl x:Name="EmployeeList" Margin="10"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding FullName}">TextBlock> DataTemplate> ItemsControl.ItemTemplate> ItemsControl> Grid>Window>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }2.WPF窗体代码
using System.Windows;namespace WpfApplication1{ ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { var emps = new[]{ new{ FullName="陈希章" }, new{ FullName="张三丰" }, new{ FullName="张无忌" } }; EmployeeList.ItemsSource = emps; } }}
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }3.WPF运行起来的效果
然后我们来看Silverlight
1. Silverlight页面标记
<UserControl x:Class="SilverlightApplication1.MainPage" 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"> <Grid x:Name="LayoutRoot" Background="White"> <ItemsControl x:Name="EmployeeList" Margin="10"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding FullName}">TextBlock> DataTemplate> ItemsControl.ItemTemplate> ItemsControl> Grid>UserControl>
2. Silverlight页面代码
using System.Windows;using System.Windows.Controls;namespace SilverlightApplication1{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { var emps = new[]{ new{ FullName="陈希章" }, new{ FullName="张三丰" }, new{ FullName="张无忌" } }; EmployeeList.ItemsSource = emps; } }}
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
3. 运行起来看的效果
是的,什么也没有。而如果我们不使用Binding语法,直接写好固定的文本值。例如下面这样
<UserControl x:Class="SilverlightApplication1.MainPage" 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"> <Grid x:Name="LayoutRoot" Background="White"> <ItemsControl x:Name="EmployeeList" Margin="10"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="Hello,World">TextBlock> DataTemplate> ItemsControl.ItemTemplate> ItemsControl> Grid>UserControl>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }则运行起来会怎么样呢?我们能看到三行数据
这说明什么问题呢?我想至少说明,绑定的操作是已经发生了,只不过没有绑定到FullName而已,或者说FullName没有数据?
好吧,那么怎么解决该问题呢?答案是在Silverlight中要用强类型的方式来保存数据。
例如下面这样的代码
using System.Windows;using System.Collections.Generic;using System.Windows.Controls;namespace SilverlightApplication1{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { Listemps = new List (){ new Employee(){FullName="陈希章"}, new Employee(){FullName="张三丰"}, new Employee(){FullName="张无忌"} }; EmployeeList.ItemsSource = emps; } public class Employee { public string FullName { get; set; } } }}
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }然后,再次运行就可以了
这样就和谐了!
【注意】这个问题有些不合情理。但我目前也没有想明白为什么,或者是不是因为某些bug导致也未可知。先解决问题吧,以后再看看。如果有哪位朋友对此有什么高见,请不吝赐教
- 关于Silverlight对匿名类型数据绑定的问题及其解决方法
- 关于数据动态绑定问题的解决方法
- silverlight绑定弱类型数据
- SilverLight学习笔记--关于使用IValueConvert对绑定数据的格式化操作
- Silverlight的数据绑定
- Silverlight 数据绑定相关问题
- vue 开发中element-ui库的switch开关绑定number类型数据不成功问题 解决方法
- WPF/Silverlight中的数据绑定问题
- Silverlight实战示例总结(兼对Object类型绑定的处理)
- 关于匿名类型的使用
- 关于动态数据绑定的问题
- 求教Eclipse关于数据绑定的问题
- 关于Spring MVC的数据绑定问题
- silverlight中对绑定数据源的要求
- 关于DataGridView绑定数据报错的解决方法
- silverlight教程 silverlight 数据绑定
- Silverlight的绑定数据(初步学习)
- SilverLight 5 数据绑定的高级话题
- jbytearray转c++byte数组
- MATLAB源程序
- MOSS 2010:Visual Studio 2010开发体验(18)——在独立的Silverlight应用程序中通过WCF访问SharePoint数据
- .COM文件
- hibernate3.0中lazy
- 关于Silverlight对匿名类型数据绑定的问题及其解决方法
- MATLAB滤波实验
- 上拉电阻下拉电阻的总结-转载
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- 域中立即强制同步客户端时钟|活动目录域windows时间服务
- MOSS 2010 : 如何启用(BDC)Business Data Connectivity——安装WCF Fix
- tcpdump常用命令
- MOSS 2010:Visual Studio 2010开发体验(21)——使用Business Connectivity Service(BCS)集成业务系统
- 表分区的几种类型及操作方法