属性名称不适用于我的datagrid列绑定

时间:2017-05-04 22:15:47

标签: wpf xaml mvvm datagrid

刚刚开始在WPF XAML中学习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的方式不正确&amp;我无法在这样一个基本级别上找到解决这个主题的帖子。

我正在尝试在DataGrid中进行双向绑定,但我想确保我先了解数据的连接方式。这就是为什么我试图将ViewModel的ObservableCollection的属性手动绑定到DataGrid中的列。

2 个答案:

答案 0 :(得分:1)

试试这个......

<DataGrid Height="100"
          ItemsSource="{Binding allIdeas}"
          AutoGenerateColumns="False"
          >
  <DataGridTextColumn Binding="{Binding IdeaID}"/>
  <DataGridTextColumn Binding="{Binding Name}"/>
</DataGrid>

答案 1 :(得分:1)

我正在修改AQuirky的答案,但他此刻似乎已经离开了。他的答案中有一个错误。

<DataGrid Height="100"
          ItemsSource="{Binding allIdeas}"
          AutoGenerateColumns="False"
          >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding IdeaID}"/>
        <DataGridTextColumn Binding="{Binding Name}"/>
    </DataGrid.Columns>
</DataGrid>