剛開始學習WPF XAML中的DataGrid。這是代碼:屬性名稱不適用於我的datagrid列綁定
型號
Public Class Idea
Public Property IdeaID() As Integer
Public Property Name() As String
End Class
視圖模型
Public Class IdeaViewModel
Implements INotifyPropertyChanged
Public Shared Property allIdeas() As New ObservableCollection(Of Idea)
Public Sub New()
For index = 1 To 10
Dim anitem As New Idea
anitem.Name = "Value " & index
allIdeas.Add(anitem)
Next
End Sub
Public ReadOnly Property AddAnItemToList() As ICommand
Get
Return New RelayCommand(AddressOf InsertAnItem)
End Get
End Property
Public Sub InsertAnItem()
Dim anItem As New Idea
anItem.Name = "Item " & allIdeas.Count()
allIdeas.Add(anItem)
End Sub
Public ReadOnly Property ClearTheList() As ICommand
Get
Return New RelayCommand(AddressOf ClearStoredList)
End Get
End Property
Public Sub ClearStoredList()
allIdeas.Clear()
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
End Class
視圖(XAML只,沒有後面的代碼)
<Window x:Class="IdeaView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:MVVM7"
Title="IdeaView" Height="500" Width="200" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<local:IdeaViewModel/>
</Window.DataContext>
<StackPanel>
<Button Margin="25" Height="50" Content="Insert an item" Command="{Binding AddAnItemToList}"/>
<Button Margin="25" Height="50" Content="Clear stored list" Command="{Binding ClearTheList}"/>
<DataGrid Height="100"
ItemsSource="{Binding allIdeas}"
AutoGenerateColumns="True"
>
</DataGrid>
<DataGrid Height="100"
AutoGenerateColumns="False"
>
<DataGridTextColumn Binding="{Binding allIdeas}"/>
<DataGridTextColumn Binding="{Binding allIdeas}"/>
</DataGrid>
</StackPanel>
</Window>
的第一個DataGrid上來的罰款。第二個DataGrid當然不是因爲您無法將列綁定到整個allIdeas
對象。我只是將代碼留在指出我知道我想要的東西像"{Binding Name}"
,但我綁定DataGrid的方式不正確&我一直無法找到一個帖子,解決這個問題在這樣一個基本水平。
我想在DataGrid中以雙向綁定的方式工作,但我想確保我理解數據如何首先連接。這就是爲什麼我試圖手動將ViewModel的ObservableCollection的屬性綁定到DataGrid中的列。
你想在第二個數據網格中有什麼? –
與第一個自動生成的網格完全相同,除了我想了解如何在XAML代碼中手動指定每列。 – Alan
Ohhhh沒錯。好。 –