如何将DataTemplate控件绑定到ObservableCollections'项目属性

时间:2014-12-11 12:25:22

标签: wpf vb.net xaml

我尝试创建自定义列表框以显示某些数据,但是我无法找到在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绑定选项给我一个错误&#34;无法解析符号X&#34;。

这样做的正确方法是什么?

更新

这是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

1 个答案:

答案 0 :(得分:0)

由于ItemsSource设置为ObservableCollection<Model.Version>,因此每个项目都属于Model.Version类型,对于每个DataContext,这将是ListBoxItem,因此{{1}你需要引用DataTemplate

的属性
Model.Version

另外,它不应该编译,但问题是当你指定<TextBlock Text="{Binding Path=Numero}"/>

ItemsSource

您应该指定GridVersion.ItemsSource = versiones

versiones.ListaVersiones

另一件事是,当您为GridVersion.ItemsSource = versiones.ListaVersiones 属性引发NotifyPropertyChanged时,您需要传递 ListaVersiones (确切的属性名称)而不是 Versiones 你现在这样做

ListaVersiones

由于您手动设置了NotifyPropertyChanged("ListaVersiones") ,因此您不需要在XAML中进行此操作

ItemsSource

您在ItemsSource="{Binding Source={StaticResource ViewModel}, Path=ListaVersiones}" 中创建的新VersionViewModel实例与您在XAML中创建的实例

Window_Loaded