Datagrid不显示DataTable的绑定值 [英] Datagrid does not show bound values from DataTable

查看:50
本文介绍了Datagrid不显示DataTable的绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataGrid没有显示绑定DataTable的值。奇怪的是,它仍然会加载容纳DataTable中所有项目所需的行数。



这是型号:



  private   string  _productkey; 
private int _price,_stock;

public string ProductKey
{
get { return _productkey;}
set
{
if (_productkey!= value
_productkey = ;

OnPropertyChanged( ProductKey);
OnPropertyChanged( Price);
OnPropertyChanged( Stock);
}
}

public int 价格
{
获取 {返回 _price;}
set
{
if (_price!= value
_price = value ;

OnPropertyChanged( Price);
}
}

public int Stock
{
get { return _stock;}
set
{
if (_ stock!= value
_stock = value ;

OnPropertyChanged( Stock);
}
}





在DataGrid中,我只是想显示ProductKey和价格,这是ViewModel:



  private 产品_product; 
private DataTable dt;

public DataTable ProductPrices
{
get
{
dt.Columns.Clear();
dt.Rows.Clear();

dt.Columns.Add( 产品密钥);
dt.Columns.Add( Price INT );

dt.Rows.Add( 101);
dt.Rows.Add( 102);
dt.Rows.Add( 201);
dt.Rows.Add( 305);

for int i = 0 ; i < dt.RowCount; i ++)
{
_product.ProductKey = dt.Rows [i ] [ 0 ]。ToString();
dt.Rows [i] [ 1 ] = _product.Price;
}

return dt;
}
}





最后,这是我的DataGrid的xaml

< pre lang =xml> < DataGrid ItemsSource = {Binding ProductPrice} >

< / DataGrid >
< DataGrid.Columns >
< DataGridTextColumn 绑定 = {x:Null} ClipBoardContentBinding = {x:Null} < span class =code-attribute> 标题 = 产品密钥 IsReadOnly = True / >
< DataGridTextColumn 绑定 = {x:Null} ClipBoardContentBinding = {x:Null} 标题 = 价格 IsReadOnly = / >
< / DataGrid.Columns >

< br $> b $ b

我错过了什么?

解决方案

你的 ItemsSource 正在寻找一个名为ProductPrice的属性,但是view-model属性被称为ProductPrice s 。我认为你的问题只是一个错字?



ProductPrices 属性看起来很奇怪 - 你正在清理每次访问该属性时重置 DataTable



您还使用模型实例来读取价格,但在您发布的代码中,价格永远不会更新。你刚刚省略了一些代码吗?



主要问题很可能是网格列上的绑定,这些绑定尚未设置:

 <   DataGridTextColumn    绑定  =  {x: Null}    ...  



需要将这些设置为要显示的列的正确绑定:

 <   DataGridTextColumn     Binding   =  {Binding Path ='Product Key'}   < span class =code-attribute> ...     /  >  
< DataGridTextColumn 绑定 = {Binding Path = Price} ... / >



NB:绑定到包含a的列名在空格中,您需要将名称用单引号括起来。由于名称不会显示在任何地方,因此将列重命名为 ProductKey 会更容易,没有空格。



您发布的代码在 DataGrid 标记之外还有 DataGrid.Columns 。我认为这是另一个错字?


My DataGrid is not showing values from a bound DataTable. Strangely, it still loads the number of rows needed to accomodate all the items in the DataTable.

Here's the model:

private string _productkey;
private int _price, _stock;

public string ProductKey
{
     get {return _productkey;}
     set
       {
          if (_productkey != value)
             _productkey = value;

          OnPropertyChanged("ProductKey");
          OnPropertyChanged("Price");
          OnPropertyChanged("Stock");
       }
}

public int Price
{
     get {return _price;}
     set
       {
          if (_price != value)
             _price= value;

          OnPropertyChanged("Price");
       }
}

public int Stock
{
     get {return _stock;}
     set
       {
          if (_stock != value)
             _stock= value;

          OnPropertyChanged("Stock");
       }
}



In the DataGrid, I just want to show the ProductKey and price, Here is the ViewModel:

private Product _product;
private DataTable dt;

public DataTable ProductPrices
{
     get
       {
          dt.Columns.Clear();
          dt.Rows.Clear();

          dt.Columns.Add("Product Key",string);
          dt.Columns.Add("Price",int);

          dt.Rows.Add("101",null);
          dt.Rows.Add("102",null);
          dt.Rows.Add("201",null);
          dt.Rows.Add("305",null);

          for (int i = 0 ; i < dt.RowCount ; i ++)
            {
               _product.ProductKey = dt.Rows[i][0].ToString();
               dt.Rows[i][1] = _product.Price;
            }

          return dt;
       }
}



finally, here's the xaml for my DataGrid

<DataGrid ItemsSource="{Binding ProductPrice}">

</DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipBoardContentBinding="{x:Null}" Header="Product Key" IsReadOnly="True"/>
<DataGridTextColumn Binding="{x:Null}" ClipBoardContentBinding="{x:Null}" Header="Price" IsReadOnly="True"/>
</DataGrid.Columns>



What did I miss?

解决方案

Your ItemsSource is looking for a property called "ProductPrice", but the view-model property is called "ProductPrices". I assume that's just a typo in your question?

The ProductPrices property looks odd - you're clearing and resetting the DataTable every time the property is accessed.

You're also using an instance of your model to read the price, but in the code you've posted, the price is never updated. Have you just omitted some of the code?

The main problem is most likely the bindings on the grid columns, which haven't been set:

<DataGridTextColumn Binding="{x:Null}" ...


These need to be set to proper bindings for the columns you want to display:

<DataGridTextColumn Binding="{Binding Path='Product Key'}" ... />
<DataGridTextColumn Binding="{Binding Path=Price}" ... />


NB: To bind to a column name that contains a space, you need to wrap the name in single quotes. Since the name isn't displayed anywhere, it would be easier to rename the column to ProductKey, without the space.

The code you've posted also has the DataGrid.Columns outside of the DataGrid tag. I assume that's another typo?


这篇关于Datagrid不显示DataTable的绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆