将文本框绑定到ObservableCollection

时间:2011-06-15 20:08:32

标签: wpf vb.net xaml binding observablecollection

我正在尝试将文本框文本属性绑定到可观察集合,并将更改报告回集合。 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

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>

什么是绑定它的正确方法?

更新
这是我希望做的,除了没有listview / itemscontrol

<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" />

解决方案

好的,学习如何正确使用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" 
             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

1 个答案:

答案 0 :(得分:0)

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