2012-09-25 74 views
0

您好我正在研究windows phone中的數據綁定屬性。我搜索了一些博客。從這些博客中瞭解到,我在ListBox中綁定了值。現在,列表框顯示了許多具有名稱,數量,日期,圖像屬性的產品。但是現在我試圖做到這一點,如果點擊列表中的任何產品,我想導航到其他頁面並在那裏顯示產品詳細信息。但在該頁面中,數據綁定不起作用。只是這些文本塊沒有顯示。我在下面列出了我的代碼。請幫我在我的代碼中找到問題。windows phone中的數據綁定問題

Transaction.cs

public class Transaction 
{ 
    public String Name { get; set; } 
    public String Date { get; set; } 
    public int Amount { get; set; } 
    public String Type { get; set; } 

    public Transaction(String name, String date, int amount,String type) 
    { 
     this.Name = name; 
     this.Date = date; 
     this.Amount = amount; 
     this.Type = type; 
    } 
} 

public class ShowItem 
{ 
    public String SelectedItemName { get; set; } 
    public String SelectedItemImage { get; set; } 
    public String SelectedItemDate { get; set; } 
    public String SelectedItemAmount { get; set; } 

    public ShowItem(String name, String date, String amount, String image) 
    { 
     this.SelectedItemName = name; 
     this.SelectedItemDate = date; 
     this.SelectedItemAmount = amount; 
     this.SelectedItemImage = image; 
    } 
} 

MainPage.xaml中

<Grid Height="530" Grid.Row="1" VerticalAlignment="Top" Margin="0,30,0,0"> 
      <ListBox Margin="0,0,0,0" Name="TransactionList"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Button Width="460" Height="150" BorderThickness="0" Click="user_click" Name="rowButton" Background="{Binding Color}" > 
          <Button.Content> 
           <StackPanel Orientation="Horizontal" Height="auto" Width="400"> 
            <Image Width="80" Height="80" Source="{Binding Type}"></Image> 
            <StackPanel Orientation="Vertical" Height="150" Margin="20,0,0,0"> 
             <StackPanel Orientation="Horizontal" Height="40"> 
              <TextBlock Width="100" FontSize="22" Text="Name :" Height="40" ></TextBlock> 
              <TextBlock Width="auto" FontSize="22" Text="{Binding Name}" Height="40" ></TextBlock> 
             </StackPanel> 
             <StackPanel Orientation="Horizontal" Height="40"> 
              <TextBlock Width="100" FontSize="22" Text="Date :" Height="40" ></TextBlock> 
              <TextBlock Width="100" FontSize="22" Text="{Binding Date}" Height="40" ></TextBlock> 
             </StackPanel> 
             <StackPanel Orientation="Horizontal" Height="40"> 
              <TextBlock Width="100" FontSize="22" Text="Amount :" Height="40" ></TextBlock> 
              <TextBlock Width="auto" FontSize="22" Text="{Binding Amount}" Height="40" ></TextBlock> 
              <TextBlock Width="auto" FontSize="22" Text=" $" Height="40" ></TextBlock> 
             </StackPanel> 
            </StackPanel> 
           </StackPanel> 
          </Button.Content> 
         </Button> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </Grid> 

MainPage.xaml.cs中

public partial class MainPage : PhoneApplicationPage 
{ 
    string[] _dates = { "19/9/1984", "25/8/1952", "27/5/1992", "4/8/1975", "10/3/2000", "22/1/2002", "23/5/2012", "25/9/1963", "13/7/1999", "15/4/1936" }; 

string[] _imageList = { "69290979.png", "acrobat-reader-cs-4.png","azureus-1.png","ClothDolls_lnx-Icons-Colored_Blue_Doll_256x256.png-256x256.png","database-27.png", 
          "documents-folder-2.png", "Front_Row_Icon.png","gear-8.png","info-chat.png","itunes-6.png","nero-smart-start-1.png", 
          "network-2.png","picasa.png","quicktime-7-red.png","twitter-bird.png","wlm-1.png" }; 


    string[] _items = { "Television", "Radio", "Fridge", "Fan", "Light", "Cup", "Plate", "Dress", "Laptop", "Mobile" }; 
    int[] _prices = { 10, 15, 20, 25, 30, 5, 8, 15, 10, 20 }; 

    List<Transaction> transactionList; 

    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 

     Loaded += new RoutedEventHandler(MainPage_Loaded); 
    } 

void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     int count = 50; 
     String DefaultName = ""; 
     String DefaultDate = ""; 
     int DefaultAmount = 0; 
     String DefaultImage = ""; 
     transactionList = new List<Transaction>(); 
     Random random = new Random(); 

     for (int i = 0; i < _items.Length; i++) 
     { 
      DefaultName = _items[i]; 
      DefaultDate = _dates[i]; 
      DefaultAmount = _prices[i]; 
      DefaultImage = "Images/" + _imageList[random.Next(0, _imageList.Length)]; 

      transactionList.Add(new Transaction(DefaultName, DefaultDate, DefaultAmount, DefaultImage)); 
     } 

     TransactionList.ItemsSource = transactionList; 
    } 

    private void user_click(object sender, RoutedEventArgs e) 
    { 
     var myData = ((Button)sender).DataContext as Transaction; 
     NavigationService.Navigate(new Uri("/DisplayProduct.xaml?Name=" + myData.Name +"&Date=" + myData.Date + "&Amount=" + myData.Amount + "&Type=" + myData.Type, UriKind.Relative)); 
    } 

} 

DisplayProduct.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <StackPanel Orientation="Vertical"> 
      <StackPanel HorizontalAlignment="Center"> 
       <Image Width="auto" Height="auto" Name="itemImage" Source="{Binding SelectedItemImage}"></Image> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <StackPanel Orientation="Vertical" Width="350" HorizontalAlignment="Left"> 
        <StackPanel Orientation="Horizontal" Margin="0,20,0,0"> 
         <TextBlock Text="Name : " Width="150" Height="70" FontSize="35" Foreground="DarkSalmon"></TextBlock> 
         <TextBlock Name="itemName" Text="{Binding SelectedItemName}" Width="auto" Height="70" FontSize="35"></TextBlock> 
        </StackPanel> 
        <StackPanel Orientation="Horizontal" Margin="0,20,0,0"> 
         <TextBlock Text="Amount : " Width="150" Height="70" FontSize="35" Foreground="DarkSalmon"></TextBlock> 
         <TextBlock Name="itemPrice" Text="{Binding SelectedItemAmount}" Width="auto" Height="70" FontSize="35"></TextBlock> 
         <TextBlock Name="currency" Text="$" Width="auto" Height="70" FontSize="35"></TextBlock> 
        </StackPanel> 
        <StackPanel Orientation="Horizontal" Margin="0,20,0,0"> 
         <TextBlock Text="Date : " Width="150" Height="70" FontSize="35" Foreground="DarkSalmon"></TextBlock> 
         <TextBlock Name="itemDate" Text="{Binding SelectedItemDate}" Width="auto" Height="70" FontSize="35"></TextBlock> 
        </StackPanel> 
       </StackPanel> 
      </StackPanel> 
     </StackPanel> 
    </Grid> 

DisplayProduct.xaml.cs

public partial class DisplayProduct : PhoneApplicationPage 
{ 
    public DisplayProduct() 
    { 
     InitializeComponent(); 
     Loaded += new RoutedEventHandler(DisplayProduct_Loaded); 
    } 

    void DisplayProduct_Loaded(object sender, RoutedEventArgs e) 
    { 
     string name = NavigationContext.QueryString["Name"]; 
     string date = NavigationContext.QueryString["Date"]; 
     string amount = NavigationContext.QueryString["Amount"]; 
     string type = NavigationContext.QueryString["Type"]; 

     ShowItem showItemClass = new ShowItem(name, date, amount, type); 
    } 

} 

回答

2
void DisplayProduct_Loaded(object sender, RoutedEventArgs e) 
{ 
    string name = NavigationContext.QueryString["Name"]; 
    string date = NavigationContext.QueryString["Date"]; 
    string amount = NavigationContext.QueryString["Amount"]; 
    string type = NavigationContext.QueryString["Type"]; 

    ShowItem showItemClass = new ShowItem(name, date, amount, type); 
    DataContext = showItemClass; 
} 

你忘了設置頁面的DataContext。它不知道它應該在哪裏得到數據顯示。

+0

謝謝..我的關於數據綁定的概念在這個示例中是正確的嗎? – Arun

+0

是的,沒關係。我會從主頁中的xaml進行數據綁定。 http://stackoverflow.com/questions/506495/how-to-properly-bind-a-listboxitem-in-wpf但你的變體也是正常的。 –

+0

好的。謝謝..... – Arun

相關問題