- 北有云溪
-
* 回复内容中包含的链接未经审核,可能存在风险,暂不予完整展示!下面的代码是我做的练习时做的代码,演示了silverlight的三种数据绑定方式,可以直接拷贝使用,希望对你有用(1)这是前台的MainPage.xaml代码:<UserControl x:Class="SilverlightApplicationLearning.MainPage" xmlns="http://schemas.m*******.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.m*******.com/winfx/2006/xaml"
xmlns:d="http://schemas.m*******.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="800" Height="600" Loaded="UserControl_Loaded_1">
<Grid HorizontalAlignment="Left" Height="590" VerticalAlignment="Top" Width="800" Loaded="Grid_Loaded_1" Margin="1,61,-1,-51">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="119*"/>
</Grid.RowDefinitions>
<TextBlock Text="姓名:" HorizontalAlignment="Left" Margin="10,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="0.303,-1.062" Grid.Row="1"/>
<TextBlock HorizontalAlignment="Left" Margin="10,80,0,0" TextWrapping="Wrap" Text="地址:" VerticalAlignment="Top" Grid.Row="1"/>
<TextBlock x:Name="lblName" HorizontalAlignment="Left" Margin="58,59,0,0" TextWrapping="Wrap" Text="{Binding Name,Mode= OneTime}" VerticalAlignment="Top" Grid.Row="1"/>
<TextBlock x:Name="lblAddress" HorizontalAlignment="Left" Margin="58,80,0,0" TextWrapping="Wrap" Text="{Binding Address,Mode=OneWay}" VerticalAlignment="Top" Grid.Row="1"/>
<Image Source="Images/1.png" HorizontalAlignment="Left" Height="46" Margin="10,5,0,0" VerticalAlignment="Top" Width="100" Grid.Row="1"/>
<Button Content="一次绑定" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="10,102,0,0" Click="Button_Click_1" Grid.Row="1"/>
<TextBlock x:Name="lblShow" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Info,Mode= TwoWay}" VerticalAlignment="Top" Margin="143,5,0,0" Height="146" Width="647" Foreground="#FFE24B4B" FontWeight="Bold" Grid.Row="1"/>
<Button Content="单向绑定" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="10,129,0,0" Click="Button_Click_2" Grid.Row="1" />
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="37.127" Margin="33.296,200.291,0,0" VerticalAlignment="Top" Width="108.883" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" Background="#FFF3F970" Grid.Row="1">
<Border.RenderTransform>
<CompositeTransform SkewY="-29.186" TranslateY="-27.957" Rotation="27.949" TranslateX="0.47"/>
</Border.RenderTransform>
<TextBlock Text="文章列表" TextWrapping="Wrap" Margin="28,9,32.883,9.127" >
</TextBlock>
</Border>
<ListBox x:Name="PostList" Height="136" VerticalAlignment="Top" VerticalContentAlignment="Bottom" Margin="15,212,10,0" ItemsSource="{Binding Posts}" Grid.Row="1">
<ListBox.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FFCD7227" Offset="1"/>
</LinearGradientBrush>
</ListBox.BorderBrush>
</ListBox>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="52" VerticalAlignment="Top" Width="382" Margin="173,-57,0,0">
<TextBlock TextWrapping="Wrap" Text="databinding" Margin="-1,1,-37,-1" FontWeight="Bold" FontSize="36" TextDecorations="Underline"/>
</Border>
</Grid>
<!--用户控制区域的大小--></UserControl>
(2)这是后台MainPage.cs代码:
using System;using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplicationLearning{
public partial class MainPage : UserControl
{
public MainPage() {
InitializeComponent();
}
public class User {
public string Name { get; set; }
public string Address { get; set; }
}
public class User1 : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Name"));
}
}
}
private string _address;
public string Address
{
get { return _address; }
set
{
_address = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Address"));
}
}
}
private string _info;
public string Info
{
get { return _info; }
set {
_info = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Info"));
}
}
}
}
public class Blog {
public List<String> Posts { get; set; }
}
User u = new User();
User1 u1 = new User1();
/// <summary>
/// 一次绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
u1.Info = "一次绑定,用来显示在页面中不变化的东西。";
lblShow.DataContext = u1;
}
private void Grid_Loaded_1(object sender, RoutedEventArgs e) {
u.Address = "宾川";
u.Name = "大刀";
lblName.DataContext = u;
lblAddress.DataContext = u;
u1.Info+= "在Silverlight 2中,支持三种模式的数据绑定。 ";
u1.Info+="1.OneTime:一次绑定,在绑定创建时使用源数据更新目标,适用于只显示数据而不进行数据的更新。 ";
u1.Info+="2.OneWay:单向绑定,在绑定创建时或者源数据发生变化时更新到目标,适用于显示变化的数据。 ";
u1.Info+="3.TwoWay:双向绑定,在任何时候都可以同时更新源数据和目标。 ";
u1.Info+="这三种模式在于绑定控件的一个属性Mode=Onetime;Mode=OneWay;Mode=TwoWay;默认使用的是:oneway。 ";
u1.Info += "姓名采用onetime模式,所以在第一次显示后即使数据源发生变化也不会被修改。 ";
u1.Info += "地址采用oneway模式,所以是要数据源u改变了它都会被更改。 ";
u1.Info += "显示框采用了twoway模式。";
lblShow.DataContext = u1;
}
/// <summary> /// 单向绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_2(object sender, RoutedEventArgs e)
{
u.Address = "大理";
u.Name = "小刀";
lblName.DataContext = u;
lblAddress.DataContext = u;
u1.Info = "OneWay:单向绑定,在绑定创建时或者源数据发生变化时更新到目标,适用于显示变化的数据。";
lblShow.DataContext = u1;
}
private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
{
Blog blog = new Blog();
blog.Posts = new List<String>
{
"条目一",
"条目二",
"条目三",
"条目四",
"条目五",
"条目六",
}; PostList.DataContext = blog;
}
}
}
- CarieVinne
-
<TextBlock Text="{Binding PropertyName}" />
- Chen
-
能详细点么??具体的代码或者??