Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据网格列dosen';我不能得到更新_C#_Wpf_Random_Timer_Datagrid - Fatal编程技术网

C# 数据网格列dosen';我不能得到更新

C# 数据网格列dosen';我不能得到更新,c#,wpf,random,timer,datagrid,C#,Wpf,Random,Timer,Datagrid,我想每3秒以随机方式更改datagrid中的列值。 我在这里引用了一个链接()当我在一个文本框上测试它时,它起了作用。 但只有双击单元格时,它才不会更新datagrid列单元格。 这是viewModel,我要更改的列是MarketPrice public class PortfolioViewModel : INotifyPropertyChanged { public Order order; private readonly Random random = new Rand

我想每3秒以随机方式更改datagrid中的列值。 我在这里引用了一个链接()当我在一个文本框上测试它时,它起了作用。 但只有双击单元格时,它才不会更新datagrid列单元格。 这是viewModel,我要更改的列是MarketPrice

 public class PortfolioViewModel : INotifyPropertyChanged
{
    public Order order;
    private readonly Random random = new Random();
     public PortfolioViewModel()
    {
        this.PortfolioViewObs = PortfolioView();


        var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
        timer.Tick += new EventHandler(Timer_Tick);
        timer.Start();

    }

 public ObservableCollection<PortfolioView> PortfolioViewObs
     {
         get;
         set;
     }

 private void Timer_Tick(object sender, EventArgs e)
     {
         for (int i = 0; i < this.PortfolioViewObs.Count; i++)
         {
             this.PortfolioViewObs[i].MarketPrice = random.Next(20, 30);    
         }
     }
PortfolioView是在WCf服务中实现的类

[DataContract]
public class PortfolioView
{
    [DataMember]
    public string Symbol { get; set; }

    [DataMember]
    public float Owned { get; set; }

    [DataMember]
    public float UnitPrice { get; set; }

    [DataMember]
    public float TotalCoast { get; set; }

    [DataMember]
    public float MarketPrice { get; set; }

    [DataMember]
    public float OrderedQuantity { get; set; }
    [DataMember]
    public float AvailabeAmount { get; set; }

}

将对
Dispatchermer
的引用存储在字段中,以防止其被垃圾回收:

public class PortfolioViewModel : INotifyPropertyChanged
{
    public Order order;
    private readonly Random random = new Random();
    private readonly DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };

    public PortfolioViewModel()
    {
        this.PortfolioViewObs = new ObservableCollection<PortfolioView>;
        timer.Tick += new EventHandler(Timer_Tick);
        timer.Start();
    }

    public ObservableCollection<PortfolioView> PortfolioViewObs { get; }

    private void Timer_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < this.PortfolioViewObs.Count; i++)
        {
            this.PortfolioViewObs[i].MarketPrice = random.Next(20, 30);
        }
    }
}
公共类PortfolioViewModel:INotifyPropertyChanged
{
公共秩序;
private readonly Random=new Random();
私有只读调度程序计时器=新调度程序{Interval=TimeSpan.FromSeconds(2)};
公共PortfolioViewModel()
{
this.PortfolioViewObs=新的可观察集合;
timer.Tick+=新事件处理程序(timer\u Tick);
timer.Start();
}
公共可观测集合PortfolioViewObs{get;}
私有无效计时器(对象发送方、事件参数)
{
for(int i=0;i

并在
PortfolioView
类中实现
INotifyPropertyChanged
接口,确保在
MarketPrice
属性设置为新值时引发
PropertyChanged
事件。

我找到了解决方案。实际上,我刚刚创建了我的模型(PortfolioView)类在wpf客户端(不像我那样来自wcf服务),因此它工作得非常好。
谢谢你们的帮助

我这么做了,但没什么问题,我还是卡住了。谢谢你的帮助response@Mah10:请向我们展示如何设置窗口的
DataContext
。同时发布
PortfolioView
类的实际实现。@Mah10:编辑您的问题。不要在注释字段中发布代码。@Mah10:Your
PortfolioView
类没有实现
INotifyPropertyChanged
,那你为什么说你按照我的建议做了?
[DataContract]
public class PortfolioView
{
    [DataMember]
    public string Symbol { get; set; }

    [DataMember]
    public float Owned { get; set; }

    [DataMember]
    public float UnitPrice { get; set; }

    [DataMember]
    public float TotalCoast { get; set; }

    [DataMember]
    public float MarketPrice { get; set; }

    [DataMember]
    public float OrderedQuantity { get; set; }
    [DataMember]
    public float AvailabeAmount { get; set; }

}
public class PortfolioViewModel : INotifyPropertyChanged
{
    public Order order;
    private readonly Random random = new Random();
    private readonly DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };

    public PortfolioViewModel()
    {
        this.PortfolioViewObs = new ObservableCollection<PortfolioView>;
        timer.Tick += new EventHandler(Timer_Tick);
        timer.Start();
    }

    public ObservableCollection<PortfolioView> PortfolioViewObs { get; }

    private void Timer_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < this.PortfolioViewObs.Count; i++)
        {
            this.PortfolioViewObs[i].MarketPrice = random.Next(20, 30);
        }
    }
}