简体   繁体   English

找不到数据绑定属性,为什么?

[英]Databinding Property not found, why?

pls help me, why this binding doesn't works, I would like to binding from the PlayerBar.xaml which datacontext is PlayerBarPresenter to PlayerBarPresenter.Card1.ImgCard like this Source = Binding{Source Card1, Path = ImgCard} than I get this exception 请帮帮我,为什么这种绑定不起作用,我想将数据上下文为PlayerBarPresenter的PlayerBar.xaml绑定到PlayerBarPresenter.Card1.ImgCard,像这样Source = Binding {Source Card1,Path = ImgCard},比我得到的异常

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). mscorlib.dll System.Windows.Data中发生类型为“ System.IO.FileNotFoundException”的第一次机会异常:错误:BindingExpression路径错误:在“ Card1”“ System.String”上找不到“ ImgCard”属性(HashCode = 949723141)。 BindingExpression: Path='ImgCard' DataItem='Card1' (HashCode=949723141); BindingExpression:路径='ImgCard'DataItem ='Card1'(HashCode = 949723141); target element is 'System.Windows.Controls.Image' (Name=''); 目标元素是'System.Windows.Controls.Image'(Name =''); target property is 'Source' (type 'System.Windows.Media.ImageSource').. 目标属性是“源”(类型为“ System.Windows.Media.ImageSource”)。

the file is found If is biding in the same property just in the TablePresenter, but I want to binding in the TablePresenter.Card1.ImgCard 如果在TablePresenter中的同一属性中出价,则找到该文件,但是我想在TablePresenter.Card1.ImgCard中进行绑定

UserControl x:Class="poki.View.PlayerBar" Data context is TablePresenter UserControl x:Class =“ 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 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 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 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");
            }
        }



    }
}

认为您的绑定表达式是错误的,请尝试如下操作:

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

You set the Source to the string "Card1" , of course it does not have any properties that you expect. 您将Source设置为字符串"Card1" ,当然它没有您期望的任何属性。 As the card is in the DataContext you will want to extend the Path ( Path=Card1.ImgCard ), never set any source when binding to the DataContext or that will be used instead of the DataContext . 由于卡位于DataContext您将需要扩展PathPath=Card1.ImgCard ),在绑定到DataContext时永远不要设置任何源,否则将使用任何源来代替DataContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM