【问题标题】:Binding textbox to ObservableCollection将文本框绑定到 ObservableCollection
【发布时间】:2011-09-15 20:40:32
【问题描述】:

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

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

更新
这是我希望做的,除了没有 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

【问题讨论】:

  • 只有在问题实际上与它有任何关系时才使用visual-studio标签。
  • 它是在visual studio中编写的...我以后会省略它。所以我应该只使用 vb.net 标签?标记使用规则是否在任何地方都有说明,这样我就不会犯其他错误?
  • 据我所知,它们不是任何此类规则,只需考虑标签是否与问题相关。情况并非如此,例如,如果 VS 设计器失败并且问题是关于如何解决该问题,那么标签将是合理的。

标签: wpf vb.net xaml binding observablecollection


【解决方案1】:

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

【讨论】:

  • 我这样做没有问题,而且这段代码也不管用。我已经能够使用 ItemSource 将文本框绑定到 listview、itemscontrol 等中的 observablecollections,但是在不使用这些容器之一的情况下直接将其绑定到文本框是新的。我该怎么做才对?
  • 您到底期望/想要什么?应用程序如何知道如何将 ObservableCollection 转换为文本?
  • 我用一个我希望做的例子更新了这个问题。我只是不想使用列表视图。
  • 还是没看懂,不好意思。不能用语言来形容吗?
  • 好的,弄清楚你对后面代码中的 Datacontext 的意思。想知道为什么不能在 XAML 中执行此操作吗?并不是说这有什么大不了,只是很有趣。
猜你喜欢
  • 1970-01-01
  • 2017-05-31
  • 2010-11-06
  • 1970-01-01
  • 2016-04-20
  • 2018-08-13
  • 1970-01-01
  • 2011-05-07
  • 2011-04-10
相关资源
最近更新 更多