2012-04-20 37 views
2

請幫我,爲什麼這個綁定不起作用,我想綁定從PlayerBar.xaml datacontext是PlayerBarPresenter到PlayerBarPresenter.Card1.ImgCard這樣Source =綁定{源Card1,路徑= ImgCard}比我得到此異常未找到Databinding Property,爲什麼?

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll System.Windows.Data Error: BindingExpression path error: 'ImgCard' property not found on 'Card1' 'System.String' (HashCode=949723141). BindingExpression: Path='ImgCard' DataItem='Card1' (HashCode=949723141); target element is 'System.Windows.Controls.Image' (Name=''); target property is 'Source' (type 'System.Windows.Media.ImageSource')..

找到該文件。如果在招標只是在TablePresenter相同的屬性,但我想在TablePresenter.Card1.ImgCard

用戶控件X結合:類=「poki.View .PlayerBar「數據上下文是TablePresenter

<Image Width="50" Height="80" Source="{Binding Source=Card1, Path=ImgCard}" RenderTransformOrigin="0.5,0.5" Canvas.Left="108.358" 
Canvas.Top="-8.349"> 

TablePresenter.cs

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using poki.View; 
using System.Windows.Media.Imaging; 
using poki.Model; 

namespace poki.Presenters 
{ 
    public class PlayerBarPresenter : PresenterBase<PlayerBar> 
    { 

     private BitmapImage playerImage; 
     public BitmapImage PlayerImage 
     { 
      get { return playerImage; } 
      set 
      { 
       playerImage = value; 
       OnPropertyChanged("PlayerImage"); 
      } 
     } 

     private Card card1; 
     public Card Card1 
     { 
      get { return card1; } 
      set 
      { 
       card1 = value; 
       OnPropertyChanged("Card1"); 
      } 
     } 



#endregion 


     public PlayerBarPresenter(PlayerBar pb) : base(pb) 
     { 
      Card1 = new Card(22); 

      Card1.S = "fasyom";   
     } 
    } 
} 

table.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; 
using Microsoft.Phone.Controls; 
using poki.Presenters; 
using System.Windows.Media.Imaging; 
using poki.Model; 

namespace poki.View 
{ 
    public partial class Table : PhoneApplicationPage 
    { 
     public TablePresenter Presenter { get { return this.DataContext as TablePresenter; } } 

     public Table() 
     { 
      this.Loaded += new RoutedEventHandler(Table_Loaded); 
      InitializeComponent(); 
     } 

     void Table_Loaded(object sender, RoutedEventArgs e) 
     { 
      DataContext = new TablePresenter(this); 

      PlayerBarPresenter a = new PlayerBarPresenter(new PlayerBar()); 


      //a.Card1.ImgCard = new BitmapImage(new Uri("/Datas/Images/1/7.png", UriKind.Relative)); 
      //a.Card2 = new BitmapImage(new Uri("/Datas/Images/1/7.png",UriKind.Relative)); 
      gridTable.Children.Add(a.View); 
     } 




    } 
} 

card.cs

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Media.Imaging; 

namespace poki.Model 
{ 
    public class Card : Notifier 
    { 
     public int card { get; set; } 

     public Card(int Card1) 
     { 
      card = Card1; 
      ImgCard = new BitmapImage(new Uri("/Datas/Images/1/7.png", UriKind.Relative)); //new BitmapImage(new Uri("/Datas/Images/Cards/" + Suit + "/" + Rank + ".png",UriKind.Relative)); 

     }   

     public int Suit 
     { 
      get { return card/13; } 
     } 

     public int Rank 
     { 
      get { return card % 13; } 
     } 


     private BitmapImage imgCard; 
     public BitmapImage ImgCard 
     { 
      get { return imgCard; } 
      set 
      { 
       imgCard = value; 
       OnPropertyChanged("ImgCard"); 
      } 
     } 



    } 
} 
+1

前請隨意設置它讀取屬性的文檔,我好像你永遠不看了'Binding.Source'怎麼能使用。 – 2012-04-20 16:47:47

回答

0

您設置Source字符串"Card1",當然它沒有任何屬性你期望的。由於該卡位於DataContext中,因此您需要擴展PathPath=Card1.ImgCard),但在綁定到DataContext時將不會設置任何源,或者將使用該值代替DataContext

5

想想你的綁定表達式是錯誤的,請嘗試這樣的:

<Image Width="50" Height="80" Source="{Binding Card1.ImgCard}" RenderTransformOrigin="0.5,0.5" Canvas.Left="108.358" Canvas.Top="-8.349"> 
+2

謝謝,它的作品 – user1272388 2012-04-20 16:49:25

+0

太棒了;請不要忘記將問題標記爲「已回答」! – 2012-04-20 17:02:18

相關問題