wpf简单的mvvm绑定不起作用

时间:2017-04-08 08:31:22

标签: wpf mvvm data-binding

我正在使用MVVM模式构建一个WPF应用程序,但我一直在测试即使是简单的绑定,我也没有得到它的工作代码基本上是这样的:

我认为问题出在xaml Bindings中 型号:

$config['protocol'] = 'sendmail';

视图模型:

public class Product
{
    private string modelNumber;
    public string ModelNumber
    {
        get { return modelNumber; }
        set { modelNumber = value; }
    }
    private string modelName;
    public string ModelName
    {
        get { return modelName; }
        set { modelName = value; }
    }
    private decimal unitCost;
    public decimal UnitCost
    {
        get { return unitCost; }
        set { unitCost = value; }
    }
    private string description;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }
    public Product(string modelNumber, string modelName,      decimal unitCost, string description)
    {
        ModelNumber = modelNumber;
        ModelName = modelName;
        UnitCost = unitCost;
        Description = description;
    }

    public static Product GetProduct()
    {
        return new Product("1","A6",20000,"Description");
    }
}

的Xaml:

  class ProductViewModel
    {
        public Product p;
        public ProductViewModel()
        {
           p= Product.GetProduct();

        }

    }

代码背后:

<Grid Name="gridProductDetails" >

    <TextBlock Margin="7">Model Number:</TextBlock>
    <TextBox Margin="5" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding p.ModelNumber}" ></TextBox>

</Grid>

1 个答案:

答案 0 :(得分:2)

请更新以下解决问题的方法:

  1. 将ProductViewModel标记为公开
  2. 将变量p转换为auto属性。 public Product p { get; set; }
  3. 此致