这里是我的DataGrid
:WPF的DataGrid绑定不更新
<DataGrid x:Name="MoonMining"
ItemsSource="{Binding MarketData.MoonMinerals, ElementName=window}">
<DataGrid.DataContext>
<local:MoonMineral/>
</DataGrid.DataContext>
<DataGrid.Columns>
.. Yes i have columns and they are irrelevant to my question .
</DataGrid.Columns>
</DataGrid>
MarketData
是包含了大部分我的程序逻辑的一类。 MoonMinerals
是在类中定义:
public class MarketData
{
private ObservableCollection<MoonMineral> _moonMinerals = new ObservableCollection<MoonMineral>();
public ObservableCollection<MoonMineral> MoonMinerals
{
get { return _moonMinerals; }
set { _moonMinerals = value; }
}
}
这是我的MoonMineral
类:
[NotifyPropertyChanged]
public class MoonMineral
{
public MoonMineral()
: this("Def", "Def")
{
}
public MoonMineral(string name, string rarity)
{
Name = name;
Rarity = rarity;
}
public string Name { get; set; }
public double Price { get; set; }
public double Volume { get; set; }
public string Rarity { get; set; }
public double TransportVolume { get; set; }
public double TransportCosts { get; set; }
public double GrossProfit { get; set; }
public double NetProfit { get; set; }
}
正如你所看到的,我使用PostSharp清理我的代码,但是当我手动实现INotifyPropertyChanged
我有同样的问题。
现在的问题是,我DataGrid
本身不更新,我在其中修改MoonMinerals
的方法手动调用:
var bindingExpression = MoonMining.GetBindingExpression(ItemsControl.ItemsSourceProperty);
if (bindingExpression != null)
bindingExpression.UpdateTarget();
我知道这不是什么大不了的事,但我想最终设法完全使用xaml将数据绑定到ui。我之前的所有尝试都是在每次更新数据时都设置了DataGrids
ItemsSource
属性。
您是否更改'MoonMinerals'属性,如果是,它会像'MoonMineral'的属性一样引发'PropertyChanged'事件? – dkozl 2014-09-01 20:05:11
我不明白MarketData在哪里.. 你可以展示这个班级吗? 为什么你的项目源绑定到ElementName = window – 2014-09-01 20:05:21
@dkozl我正在使用PostSharp为我做这件事,但是,我尝试了手动提高它也没有工作。 – Krzaku 2014-09-01 20:07:49