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

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"];}

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

相关推荐