微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight数据绑定

(1单项绑定)

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,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多项绑定)

<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",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",6)));
// Set the data context for the combo Box.
ComboBox1.DataContext = MyMusic;
}
(3使用dataTemplate格式化绑定数据)

<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" />
<TextBlock Text="{Binding Name}" Margin="2" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
(3全局使用)

<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"/>
<TextBlock FontStyle="Italic" Text="{Binding Name}" Margin="5,0"/>
<TextBlock Text="{Binding ReleaseDate}" Margin="5,0" />
</StackPanel>
</Grid>

C#

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

<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">
<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" />
<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,System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐