Wpf 将文本框绑定到ObservableCollection

Wpf 将文本框绑定到ObservableCollection,wpf,vb.net,xaml,binding,observablecollection,Wpf,Vb.net,Xaml,Binding,Observablecollection,我试图将textbox文本属性绑定到可观察的集合,并将更改报告回集合。ContactLog属性从呼叫页面设置。 这就是我所拥有的,但它不起作用 Imports System.ComponentModel Imports System.Collections.ObjectModel Public Class cCustomerContactLogFull Property ContactLog As ocContactLogs End Class Public Class ocCon

我试图将textbox文本属性绑定到可观察的集合,并将更改报告回集合。ContactLog属性从呼叫页面设置。
这就是我所拥有的,但它不起作用

Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class cCustomerContactLogFull
    Property ContactLog As ocContactLogs

End Class

Public Class ocContactLogs
    Implements INotifyPropertyChanged

    Private _ContactLogID As Integer
    Private _CustomerID As Integer
    Private _ContactMsg As String

    Private _Changed As Boolean

    Private _ContactLogIDChanged As Boolean
    Private _CustomerIDChanged As Boolean
    Private _ContactMsgChanged As Boolean

    Public Sub New(ByVal contactlogid As Integer, ByVal customerid As Integer, ByVal contactmsg As String)

        _ContactLogID = contactlogid
        _CustomerID = customerid
        _ContactMsg = contactmsg

        _Changed = False

        _ContactLogIDChanged = False
        _CustomerIDChanged = False
        _ContactMsgChanged = False
    End sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Protected Overridable Sub OnPropertyChanged(ByVal Propertyname As String)
        If Not Propertyname.Contains("Changed") Then
            Changed = True
        End If
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Propertyname))
    End Sub

    Public Property Changed() As Boolean
        Get
            Return _Changed
        End Get
        Set(ByVal value As Boolean)
            If _Changed <> value Then
                _Changed = value
                OnPropertyChanged("Changed")
            End If
        End Set
    End Property

    Public Property ContactLogID() As Integer
        Get
            Return _ContactLogID
        End Get
        Set(ByVal value As Integer)
            If _ContactLogID <> value Then
                _ContactLogID = value
                OnPropertyChanged("ContactLogID")
            End If
        End Set
    End Property

    Public Property CustomerID() As Integer
        Get
            Return _CustomerID
        End Get
        Set(ByVal value As Integer)
            If _CustomerID <> value Then
                _CustomerID = value
                OnPropertyChanged("CustomerID")
            End If
        End Set
    End Property

    Public Property ContactMsg() As String
        Get
            Return _ContactMsg
        End Get
        Set(ByVal value As String)
            If _ContactMsg <> value Then
                _ContactMsg = value
                OnPropertyChanged("ContactMsg")
            End If
        End Set
    End Property
End Class

您已将DataContext设置为字符串
“ContactLog”
。这可能不是您想要的-您应该在代码中创建和设置DataContext,可能是在构造函数中。

仅当问题与此相关时才使用visual studio标记。它是在visual studio中编写的。。。以后我就不提了。所以我应该只使用vb.net标记?标签使用规则是否在任何地方都有说明,这样我就不会犯其他错误?据我所知,它们不是任何这样的规则,只要想想标签是否与问题相关。这里的情况并非如此,例如,如果VS设计器失败,问题是如何修复它,那么标签将是合理的。我这样做没有问题,而且这段代码无论如何都不起作用。我已经能够使用ItemSource将文本框绑定到listview、itemscontrol等中的observablecollections,但是直接绑定到文本框而不使用这些容器是新的。我应该怎样做才对?你到底期望/想要什么?应用程序如何知道tp如何将一个ObservableCollection转换为文本?我用一个我希望做的例子更新了这个问题。我只是不想使用listview。还是不明白,抱歉。你们不能用文字来描述它吗?好的,弄清楚你们在代码隐藏中使用Datacontext的意思。想知道为什么不能在XAML中实现它?这不是什么大事,只是有趣而已。
<UserControl x:Class="cCustomerContactLogFull"
             x:Name="cCustomerContactLogFull"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             DataContext="ContactLog"
             d:DesignHeight="385" d:DesignWidth="657" Height="482" Width="657">
    <Grid>
        <ScrollViewer Grid.Column="1" Grid.Row="2" Name="ScrollViewer1" Margin="2" VerticalScrollBarVisibility="Auto">
            <TextBox Text="{Binding ContactMsg}" SpellCheck.IsEnabled="True" AcceptsReturn="True" Name="txtContactMsg" TextWrapping="WrapWithOverflow" />
        </ScrollViewer>
    </Grid>
</UserControl>
<UserControl.Resources>
    <DataTemplate x:Key="centralTile">
        <Grid Background="WhiteSmoke" Width="290" Margin="0,0,0,5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="400*" />
                <ColumnDefinition Width="40" />
            </Grid.ColumnDefinitions>
            <StackPanel Margin="0" HorizontalAlignment="Left" Grid.Column="0" >
                <TextBlock Text="{Binding ContactShort}" Margin="0,0,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
                <TextBlock Text="{Binding Header}" Margin="0,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
                <TextBlock Text="{Binding ShortMsg}" Margin="10,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            </StackPanel>
        </Grid>
    </DataTemplate>
    <l:PlainView x:Key="tileView" ItemTemplate="{StaticResource centralTile}" ItemWidth="300" />
</UserControl.Resources>

    <ListView ItemsSource="{Binding ElementName=cCustomerContactInfo, Path=contactlogs}" View="{StaticResource tileView}" Name="lstContactLogs" Margin="0,0,5,28" Grid.Row="1" Grid.Column="2" />
<UserControl x:Class="cCustomerContactLogFull"
             x:Name="cCustomerContactLogFull"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="385" d:DesignWidth="657" Height="482" Width="657">
    <UserControl.Resources>
        <DataTemplate x:Key="textBoxTemplate">
            <TextBox Text="{Binding Path=Value}" Width="100"></TextBox>
        </DataTemplate>

        <DataTemplate x:Key="checkBoxTemplate">
            <CheckBox IsChecked="{Binding Path=Value}" IsThreeState="False"></CheckBox>
        </DataTemplate>

    </UserControl.Resources>
    <Grid x:Name="icl">
        <ScrollViewer Grid.Column="1" Grid.Row="2" Name="ScrollViewer1" Margin="2" VerticalScrollBarVisibility="Auto">
            <TextBox Text="{Binding Path=ContactMsg}" SpellCheck.IsEnabled="True" AcceptsReturn="True" Name="txtContactMsg" TextWrapping="WrapWithOverflow" />
        </ScrollViewer>
    </Grid>
</UserControl>
Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class cCustomerContactLogFull
    Property CustomerID As Integer
    Property ContactLog As ocContactLogs

    Private Sub cCustomerContactLogFull_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If Not ContactLog Is Nothing Then
            icl.DataContext = ContactLog
        End If
    End Sub
End Class