【发布时间】:2014-12-11 12:25:22
【问题描述】:
我正在尝试创建一个自定义列表框来显示一些数据,但是我找不到在 DataTemplate 控件和项目属性之间设置绑定的正确方法。
我有以下 POCO 类:
Namespace Model
Public Class Version
Property Numero As String
Property Checksum As String
End Class
End Namespace
还有这个 ViewModel 类:
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Namespace ViewModel
Public Class VersionViewModel
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _ListaVersiones As ObservableCollection(Of Model.Version)
Public Property ListaVersiones As ObservableCollection(Of Model.Version)
Get
Return _ListaVersiones
End Get
Set(value As ObservableCollection(Of Model.Version))
_ListaVersiones = value
NotifyPropertyChanged("Versiones")
End Set
End Property
Private Sub NotifyPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(propertyName))
End Sub
End Class
End Namespace
我正在尝试在以下 XAML 中使用此 ViewModel:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Projeto.ViewModel"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<vm:VersionViewModel x:Key="ViewModel"/>
<Style
BasedOn="{StaticResource {x:Type ListBox}}"
TargetType="ListBox"
x:Key="EstiloGridVersion">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid Background="Blue">
<Border Background="Red" x:Name="border" BorderBrush="Gray" CornerRadius="6" Height="30" BorderThickness="1">
<!-- The error is in the Binding Path -->
<TextBlock Text="{Binding Path=ListaVersiones.Numero}"/>
</Border>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ListBox x:Name="GridVersion" Style="{StaticResource EstiloGridVersion}" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=ListaVersiones}">
</ListBox>
</Window>
TextBlock 绑定选项给我一个错误“无法解析符号 X”。
这样做的正确方法是什么?
更新
这里是 Window Loaded 事件代码:
Class MainWindow
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim versiones As New VersionViewModel
versiones.ListaVersiones = New ObjectModel.ObservableCollection(Of Model.Version)()
versiones.ListaVersiones.Add(New Model.Version() With {.Numero = "07.0201", .Checksum = "0450a4s6540a6s5006a5s4"})
versiones.ListaVersiones.Add(New Model.Version() With {.Numero = "07.0207", .Checksum = "243jkh234jh23j3406a5s4"})
GridVersion.ItemsSource = versiones
End Sub
End Class
【问题讨论】:
-
ItemsSource是ListaVersiones所以每个项目都是Version类型所以<TextBlock Text="{Binding Path=Numero}"/>应该可以工作 -
它在设计时给出了同样的错误:无法解析符号 Numero。当应用程序执行时,它只是关闭,没有崩溃消息(同时使用 ListaVersiones.Numero 和 Numero)。
-
它可能会在设计时给出错误(有时设计师在解决正确的上下文时遇到问题),但它应该在运行时工作。如果您的应用程序关闭,我认为是出于不同的原因,那么此绑定存在问题
-
同样,当您为
ListaVersiones属性提出NotifyPropertyChanged时,您需要传递 ListaVersiones 而不是像现在那样传递 Versiones跨度> -
我已经为整个应用添加了其余代码。我找不到在不显示任何错误的情况下自动关闭的理由。修复了 NotifyPropertyChanged 但应用仍然关闭。