开发者

Binding textbox to ObservableCollection

开发者 https://www.devze.com 2023-03-13 02:16 出处:网络
I\'m trying to get bind a textbox text property to an observable collection, and report changes back to the collection. The ContactLog property gets set from the calling page.

I'm trying to get bind a textbox text property to an observable collection, and report changes back to the collection. The ContactLog property gets set from the calling page.

This is what I have, but it doesn't work.

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

    Publi开发者_Python百科c 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>

Whats the right way to bind this?

UPDATE

This is what I'm hoping to do, except without the 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" />

Solution

Ok, learning how to properly use DataContext, this appears to work, and it's two way, so bonus.

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>

Code Behind:

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


You have set your DataContext to the string "ContactLog". This is probably not what you want - you should create and set your DataContext in code, probably in the constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号