2016-07-25 165 views
1

如何將ObservableCollection<updateData>updateCollection綁定到DataGrid?我嘗試了幾個解決方案,但似乎沒有工作,因爲行被添加到集合中,但沒有顯示在網格上。我試圖綁定到類只,然後我可以添加行,但當我嘗試編輯他們時,我得到錯誤'EditItem' is not allowed for this view。網格是以下將ObservableCollection綁定到DataGrid

<DataGrid Name="dgv" Grid.ColumnSpan="7" AutoGenerateColumns="False" ItemsSource="{Binding updateCollection}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False"> 
<DataGrid.Columns> 
    <DataGridComboBoxColumn Header="Hour" SelectedValueBinding="{Binding Active}" ItemsSource="{StaticResource hoursList}" DisplayMemberPath="Key" SelectedValuePath="Value"/> 
    <DataGridComboBoxColumn Header="Origin" SelectedValueBinding="{Binding Origin}" ItemsSource="{StaticResource originList}" DisplayMemberPath="Key" SelectedValuePath="Value"/> 
    <DataGridTextColumn Header="P" Binding="{Binding Path=Price}"/> 
    <DataGridTextColumn Header="Q" Binding="{Binding Path=Quantity}"/> 
</DataGrid.Columns> 

而且updateData類如下:

public class updateData 
{ 
    public string Price { get; set; } 
    public string Quantity { get; set; } 
    public string Origin { get; set; } 
    public string Hour { get; set; } 
} 
+2

DataContext DataGrid使用什麼? – ASh

+0

你見過這個:http://stackoverflow.com/questions/18578254/datagrid-edition-edititem-is-not-allowed-for-this-view-when-bound-to-a-wpf-da? – Silvinus

+0

@ASh我試着設置'dgv.DataContext = updateCollection',但它沒有改變任何東西。 – Hauba

回答

3

做過什麼,看起來是正確的,但如果你錯過了一個單一的東西,在DataContext ...什麼都不會工作。 這裏只爲你一個例子:

這是您的型號:

public class updateData 
{ 
    public string Price { get; set; } 
    public string Quantity { get; set; } 
    public string Origin { get; set; } 
    public string Hour { get; set; } 
} 

需要注意的是,如果你想告訴你認爲事情已經改變,你必須實現INotifyPropertyChanged接口。

這是您的視圖模型:

public class updateDataVm 
{ 
    public ObservableCollection<updateData> updateCollection { get; set; } 

    public updateDataVm() 
    { 
     updateCollection = new ObservableCollection<updateData>(); 
    } 
} 

最後這裏是你的視圖(請注意,我已經改變ItemsSourceItemsSource="{Binding}"):

<Grid> 
    <DataGrid Name="dgv" Grid.ColumnSpan="7" AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False"> 
    <DataGrid.Columns> 
     <DataGridComboBoxColumn Header="Hour" SelectedValueBinding="{Binding Active}" ItemsSource="{StaticResource hoursList}" DisplayMemberPath="Key" SelectedValuePath="Value"/> 
     <DataGridComboBoxColumn Header="Origin" SelectedValueBinding="{Binding Origin}" ItemsSource="{StaticResource originList}" DisplayMemberPath="Key" SelectedValuePath="Value"/> 
     <DataGridTextColumn Header="P" Binding="{Binding Path=Price}"/> 
     <DataGridTextColumn Header="Q" Binding="{Binding Path=Quantity}"/> 
    </DataGrid.Columns> 
</DataGrid> 
</Grid> 

在你的窗口(或一般控制):

public partial class MainWindow : Window 
{ 
    public updateDataVm collection; 
    public MainWindow() 
    { 
     InitializeComponent(); 

     collection = new updateDataVm(); 
     DataContext = collection; 
    } 
} 
相關問題