silverlight数据绑定

(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 C ollectionViewSource { 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();
}
}


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值