这应该为您工作。让我知道它是否有帮助:
XAML:
var data_point={
x:dateObject,y:parseFloat(val.sa_quantity)
};
datapoints.push(data_point);
背后的代码(.xaml.cs):
var chartData = [];
var ctxR = document.getElementById("data_chart").getContext('2d');
var myChart = new Chart(ctxR,{
type: 'scatter',data: {
labels:dates,datasets: [{
label: 'Predicted Sales($)',data: datapoints
}]
},responsive: true,title:{
display: true,text:"Chart.js Time Scale"
},scales:{
xAxes: [{
type: 'time',time: {
displayFormats: {
'millisecond': 'MMM DD','second': 'MMM DD','minute': 'MMM DD','hour': 'MMM DD','day': 'MMM DD','week': 'MMM DD','month': 'MMM DD','quarter': 'MMM DD','year': 'MMM DD',},round: 'day',unit: 'day',}
}]
}
});
myChart.update();
ViewModel:(MainWindowViewModel.cs)
<Window>
<Grid>
<Datagrid ItemsSource="{Binding idlessTicketList }" SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="False"
CanUserAddRows="True" CanUserDeleteRows="False" AutoGenerateColumns="False">
//ColumnDefinition...etc
<DataGridTextColumn Header="TicketNumber" Binding="{Binding TicketNumber}" />
<DataGridTextColumn Header="TicketCustomer" Binding="{Binding TicketCustomer}"
</Datagrid>
<Button name="ThisIsYourUpdateButton" Command="{Binding UpdateMyTicket}" Width="200" Content="Update me now"/>
</Grid>
</Window>
添加public MainWindow()
{
InitializeComponent(); //parses the XAML...
DataContext = new MainWindowViewModel(); //outsources the view code to the
//"controller" to make the view only display and nothing else...
}
类,如下:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace YourNameSpace
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public ICommand UpdateMyTicket => new DelegateCommand<object>(ExecuteUpdateTicket);
public Ticket TicketInstance {get;set;}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<Ticket> _idlessTicketList;
public ObservableCollection<Ticket> idlessTicketList
{
get { return _idlessTicketList; }
set
{
_idlessTicketList = value;
OnPropertyChanged("idlessTicketList");
}
}
//Constructor
public MainWindowViewModel()
{
idlessTicketList = new ObservableCollection<Ticket>();
}
public void ExecuteUpdateTicket(obj param)
{
//if the button is clicked you update your Ticket class properties here!
}
}
}
,
使用单独的View-Model类初始化observablecollection,可以正确显示空行。
但是数据不再更新。
这是我所做的:
我将新类添加为新的命名空间
xmlns:vm =“ clr-namespace:nsMainWindowViewModel”
绑定是这样写的:
<DataGrid ItemsSource="{Binding Path=idlessTicketList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Mode=TwoWay,Path=ticketCustomer,UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Binding="{Binding Mode=TwoWay,Path=ticketText,Path=ticketTime,UpdateSourceTrigger=PropertyChanged,StringFormat=\{0:n2\}}"/>
</DataGrid.Columns>
</Datagrid>
通过此实现,在应用程序启动时调用PropertyChangedEventHandler,但不会更改元素。
绑定必须如何编写?
对于我尝试过的datagrid:
{Binding Path=(idlessTicketList),UpdateSourceTrigger=PropertyChanged}
{Binding Path=(vm:idlessTicketList),UpdateSourceTrigger=PropertyChanged}
{Binding Path=(vm:MainWindowViewModel.idlessTicketList),UpdateSourceTrigger=PropertyChanged}
以及我尝试过的列;
{Binding Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}
{Binding Mode=TwoWay,Path=(vm:ticketCustomer),Path=(vm:MainWindowViewModel.ticketCustomer),UpdateSourceTrigger=PropertyChanged}
调试结束后,我可以看到最初调用了PropertyChange方法,但是在编辑单个元素之后却没有。
必须如何定义绑定才能在另一个命名空间中更新te observableCollection?
预先感谢
本文链接:https://www.f2er.com/3121191.html