gpt4 book ai didi

wpf - 属性名称不适用于我的数据网格列绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 11:03:26 25 4
gpt4 key购买 nike

刚开始学习 WPF XAML 中的 DataGrid。这是代码:

模型

Public Class Idea
Public Property IdeaID() As Integer
Public Property Name() As String
End Class

View 模型
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 当然不会,因为你不能将一列绑定(bind)到整个 allIdeas目的。我只是留下该代码以指出我知道我想要 "{Binding Name}" 之类的东西。 ,但是我绑定(bind) DataGrid 的方式不正确,而且我无法找到在如此基本的级别上解决此主题的帖子。

我正在尝试在 DataGrid 中进行双向绑定(bind),但我想确定我首先了解数据是如何连接的。这就是我尝试手动将 ViewModel 的 ObservableCollection 的属性绑定(bind)到 DataGrid 中的列的原因。

最佳答案

我正在修改 AQuirky 的答案,但他现在似乎已经走神了。他的回答有误。

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

关于wpf - 属性名称不适用于我的数据网格列绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43793630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com