silverlight数据绑定

原创 2012年03月21日 12:47:56

(1单项绑定)

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox VerticalAlignment="Top" IsReadOnly="True" Margin="5"
TextWrapping="Wrap" Height="120" Width="400"
Text="{Binding}" x:Name="textBox1" />
</Grid>

C#

// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context to a new Recording.
textBox1.DataContext = new Recording("Chris Sells", "Chris Sells Live",
new DateTime(2008, 2, 5));
}
// A simple business object
public class Recording
{
public Recording() { }
public Recording(string artistName, string cdName, DateTime release)
{
Artist = artistName;
Name = cdName;
ReleaseDate = release;
}
public string Artist { get; set; }
public string Name { get; set; }
public DateTime ReleaseDate { get; set; }
// Override the ToString method.
public override string ToString()
{
return Name + " by " + Artist + ", Released: " + ReleaseDate.ToShortDateString();
}
}
(2多项绑定)

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ComboBox x:Name="ComboBox1" ItemsSource="{Binding}"
Foreground="Black" FontSize="18" Height="50" Width="400"/>
</Grid>
 

C#

public ObservableCollection<Recording> MyMusic = new ObservableCollection<Recording>();
public Page()
{
InitializeComponent();
// Add items to the collection.
MyMusic.Add(new Recording("Chris Sells", "Chris Sells Live",
new DateTime(2008, 2, 5)));
MyMusic.Add(new Recording("Luka Abrus",
"The Road to Redmond", new DateTime(2007, 4, 3)));
MyMusic.Add(new Recording("Jim Hance",
"The Best of Jim Hance", new DateTime(2007, 2, 6)));
// Set the data context for the combo box.
ComboBox1.DataContext = MyMusic;
}
(3使用dataTemplate格式化绑定数据)

XAML

<ComboBox x:Name="ComboWithTemplate" Margin="5"
Width="450" Height="50" HorizontalAlignment="Left"
ItemsSource="{Binding}" Foreground="Black" FontSize="18" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Artist:" Margin="2" />
<TextBlock Text="{Binding Artist}" Margin="2" />
<TextBlock Text="CD:" Margin="10,2,0,2" />
<TextBlock Text="{Binding Name}" Margin="2" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
(3全局使用)

XAML

<Grid x:Name="LayoutRoot" Background="Transparent">
<!--The UI for the details view-->
<StackPanel x:Name="RecordingDetails">
<TextBlock FontWeight="Bold" Text="{Binding Artist}" Margin="5,0,0,0"/>
<TextBlock FontStyle="Italic" Text="{Binding Name}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding ReleaseDate}" Margin="5,0,0,0" />
</StackPanel>
</Grid>

C#

//ComboWithTemplate.DataContext = MyMusic;
LayoutRoot.DataContext = new CollectionViewSource { Source = MyMusic };
(4  格式化转化绑定数据)

XAML

<phone:PhoneApplicationPage
x:Class="MyExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyExample"
...
>
<phone:PhoneApplicationPage.Resources>
<local:StringFormatter x:Key="StringConverter"/>
</phone:PhoneApplicationPage.Resources>
...
<!--ContentPanel - place content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ComboBox x:Name="ComboWithTemplate" Margin="5"
Width="450" Height="50" HorizontalAlignment="Left"
ItemsSource="{Binding}" Foreground="Black" FontSize="18" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Artist:" Margin="2" />
<TextBlock Text="{Binding Artist}" Margin="2" />
<TextBlock Text="CD:" Margin="10,2,0,2" />
<TextBlock Text="{Binding Name}" Margin="2" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<!--The UI for the details view-->
<StackPanel x:Name="RecordingDetails">
<TextBlock Text="{Binding Artist}" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding ReleaseDate,
Converter={StaticResource StringConverter},
ConverterParameter=Released: \{0:d\}}" />
</StackPanel>
</StackPanel>
...
</phone:PhoneApplicationPage>

C#

public class StringFormatter : IValueConverter
{
// This converts the value object to the string to display.
// This will work with most simple types.
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
// Retrieve the format string and use it to format the value.
string formatString = parameter as string;
if (!string.IsNullOrEmpty(formatString))
{
return string.Format(culture, formatString, value);
}
// If the format string is null or empty, simply
// call ToString() on the value.
return value.ToString();
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}


















相关文章推荐

Silverlight之MVVM模式简单介绍项目(数据绑定)

很久没有更新博客了,今天向大家介绍一下Silverlight MVVM模式的使用。 MVVM即Model-View-ViewModel模式,它是一种轻量级的,灵活的方式分离数据实体与视图之间的关系,可...

silverlight数据绑定模式TwoWay,OneWay,OneTime的研究

asp.net开发中,数据绑定是一个很简单的概念,控件与数据绑定后,控件可以自动把数据按一定的形式显示出来。(当然控件上的值改变后,可以通过提交页面表单,同时后台服务端代码接收新值更新数据) sil...
  • toddhan
  • toddhan
  • 2012年02月12日 15:16
  • 452

Silverlight 树形控件用法(数据绑定、右键菜单)

1、树形控件定义如下:

Mvvm Light Toolkit for wpf/silverlight系列之数据绑定

Mvvm的框架的实现依赖与完善的数据绑定机制,因此熟练使用mvvm就必须熟练掌握WPF/SL的数据绑定机制。下面我们看看mvvm数据绑定与传统的.net控件使用方式有什么不一样;一、给控件属性赋值首先...

Silverlight数据绑定/IValueConverter

先回忆一下aspx中的处理: 在aspx中,可以直接在后台定义一个变量,然后前台就可以用来将其"绑定"html控件上,比如下面这样,实在是很方便: 代码 using System...

SilverLight学习笔记之三数据绑定方式(上)

在 Silverlight 中支持3种绑定:OneWay, TwoWay, OneTime. 默认是 OneWay. 其中 OneWay 表示仅仅从数据源绑定到目标(通常是 UI 对象),单向的; ...
  • cpcpc
  • cpcpc
  • 2011年10月24日 13:42
  • 761

Silverlight中的DataContext和ItemsSource 数据绑定

在Silverlight中我们经常看到数据绑定源由DataContext或者ItemsSource指定,那么他们之间的区别有哪些呢?     DataContext:从英文字母意思是数据上下文,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:silverlight数据绑定
举报原因:
原因补充:

(最多只允许输入30个字)