silverlight 数据邦定并实现页面传值

范型类提供数据源

using System;
using System.Collections.Generic;
using System.Linq;

namespace CustomerUriApp
{
    public class Customers
    {
        public List<Customer> GetAllCustomers()
        {
            List<Customer> c = new List<Customer>();
            c.Add(new Customer()
                { CustomerId = 1,
                CompanyName = "Microsoft" });
            c.Add(new Customer()
                { CustomerId = 2,
                CompanyName = "Google" });
            c.Add(new Customer()
                { CustomerId = 3,
                CompanyName = "Apple" });
            return c;
        }

    

      public Customer GetCustomer(int customerId)
        {
            var customer =
                           from c in GetAllCustomers()
                           where c.CustomerId == customerId
                           select c;

            return customer.First();
        }


    }

    public class Customer
    {
        public int CustomerId { get; set; }
        public string CompanyName { get; set; }
    }
}

邦定数据控件

<uriMapper:UriMapping Uri="Customer/{customerId}"                                        
       MappedUri="/Views/CustomerDetails.xaml?customerId={customerId}" />

 

<ItemsControl x:Name="CustomersList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <HyperlinkButton FontSize="24"
                            Content="{Binding CompanyName}"
                            Tag="{Binding CustomerId}" 
               Click="HyperlinkButton_Click" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

 页面加载时邦定数据

public Home()
{
    InitializeComponent();
    Loaded += new RoutedEventHandler(Home_Loaded);
}

 

void Home_Loaded(object sender, RoutedEventArgs e)
{
    Customers c = new Customers();
    CustomersList.ItemsSource = c.GetAllCustomers();
}

 

hyperlinkbutton click 事件

 


private void HyperlinkButton_Click
    (object sender, RoutedEventArgs e)
{
    HyperlinkButton hyperlink = sender as HyperlinkButton;
    string customerId = hyperlink.Tag.ToString();

    this.NavigationService.Navigate
        (new Uri
            (string.Format("Customer/{0}", customerId), UriKind.Relative)); //传递customerid


}

 

  CustomerDetails.xaml Page 接收传递参数

<StackPanel>
    <TextBlock x:Name="CustomerId" FontSize="24"></TextBlock>
</StackPanel>

 protected override void OnNavigatedTo(NavigationEventArgs e)
{
    CustomerId.Text = this.NavigationContext.QueryString["customerId"];
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值