代码之家  ›  专栏  ›  技术社区  ›  BillVo

绑定Silverlight 3 dataInput的描述属性:DescriptionViewer

  •  1
  • BillVo  · 技术社区  · 16 年前

    数据输入:标签 数据输入:描述查看器 控件通过绑定到文本框的功能提供了一个有用的功能。与其用System.ComponentModel.DataAnnotations.DisplayAttribute硬编码标签和描述的文本内容,我更愿意将这些属性绑定到包含管理员可编辑文本的类的属性。这适用于Label,但不适用于DescriptionViewer。

    具体来说,这个标签的作用是:

    <dataInput:Label Grid.Row="00" Grid.Column="0" 
    Target="{Binding ElementName=inpStatus}"
    Content="{Binding StatusLabel}"  IsRequired="true" />
    

    <dataInput:DescriptionViewer Grid.Row="00" Grid.Column="3" 
    Target="{Binding ElementName=inpStatus}" 
    Name="dvBound2" Description="{Binding Path=StatusDescription}" /> 
    

    当我移除对文本框的绑定时,将显示DescriptionViewer:

    <dataInput:DescriptionViewer Grid.Row="00" Grid.Column="2" 
    Name="dvBound1" Description="{Binding Path=StatusDescription}" /> 
    

    是否可以将描述查看器绑定到文本框和其他描述源?

    提前谢谢!

    主页.xaml:

    <UserControl x:Class="DataInputDemo.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"  
        xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"
        xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Grid x:Name="LayoutRoot">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="142"></ColumnDefinition>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="24"></ColumnDefinition>
                <ColumnDefinition Width="24"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="30"></RowDefinition>
            </Grid.RowDefinitions>
    
            <dataInput:Label             Grid.Row="00" Grid.Column="0" 
                Target="{Binding ElementName=inpStatus}"
                Content="{Binding StatusLabel}"  IsRequired="true" />
    
            <TextBox                     Grid.Row="00" Grid.Column="1" 
                Text="{Binding Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True, Path=Status}"
                Name="inpStatus" MaxLength="025" 
                BindingValidationError="_BindingValidationError" />
    
            <dataInput:DescriptionViewer Grid.Row="00" Grid.Column="2" 
                Name="dvBound1" Description="{Binding Path=StatusDescription}" />
    
            <!-- Following DescriptionViewer is not shown. -->
            <dataInput:DescriptionViewer Grid.Row="00" Grid.Column="3" 
                Target="{Binding ElementName=inpStatus}" 
                Name="dvBound2" Description="{Binding Path=StatusDescription}" />
    
            <dataInput:ValidationSummary Grid.Row="01" Grid.Column="0" 
                Grid.ColumnSpan="4" Name="VS1" />
    
        </Grid>
    </UserControl>
    

    主页.xaml.vb:

    Partial Public Class MainPage
        Inherits UserControl
    
        Public myDataClass = New DataClass
    
        Public Sub New()
            InitializeComponent()
            myDataClass.status = "Default Status"
            myDataClass.StatusLabel = "Status Label"
            myDataClass.StatusDescription = "Status Description"
            LayoutRoot.DataContext = myDataClass
        End Sub
    
        Private Sub _BindingValidationError(ByVal sender As Object, ByVal e As ValidationErrorEventArgs)
        End Sub
    
    End Class
    

    数据类.vb:

    Imports System.Collections.ObjectModel
    Imports System.ComponentModel
    Imports System.Runtime.Serialization
    Imports System.ComponentModel.DataAnnotations
    Imports System.Windows.Controls
    
    Public Class DataClass
    
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler _
            Implements INotifyPropertyChanged.PropertyChanged
    
        Private Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub
    
        Function ValidateEntry(ByVal fieldname As String, ByRef value As Object) As Object
            Dim ctx As New ValidationContext(Me, Nothing, Nothing)
            ctx.MemberName = fieldname
            Validator.ValidateProperty(value, ctx)
            Return value
        End Function
    
        <DataMember()> _
        <Required()> _
        <StringLength(100)> _
        Public Property Status() As String
            Get
                Return _Status
            End Get
            Set(ByVal value As String)
                _Status = ValidateEntry("Status", value)
                NotifyPropertyChanged("")
            End Set
        End Property
        Private _Status As String
    
        <DataMember()> _
        Public Property StatusLabel() As String
            Get
                Return _StatusLabel
            End Get
            Set(ByVal value As String)
                _StatusLabel = value
                NotifyPropertyChanged("")
            End Set
        End Property
        Private _StatusLabel As String
    
        <DataMember()> _
        Public Property StatusDescription() As String
            Get
                Return _StatusDescription
            End Get
            Set(ByVal value As String)
                _StatusDescription = value
                NotifyPropertyChanged("")
            End Set
        End Property
        Private _StatusDescription As String
    
    End Class
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Spasas    16 年前

    我对使用此标记的DescriptionViewer也有同样的问题:

    <TextBox x:Name="UserNameTextBox" Text="{Binding UserName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=Explicit}" Grid.Column="1" Grid.Row="1"></TextBox>
    <dataInput:DescriptionViewer Target="{Binding ElementName=UserNameTextBox}" Description="{Binding Path=CreateUserResources.UserNameDescription, Source={StaticResource GlobalResources}}" Grid.Column="2" Grid.Row="1"></dataInput:DescriptionViewer>
    

    在将描述绑定到其资源之前,它是标记中的一个纯字符串,并且确实起作用。

    <TextBox x:Name="UserNameTextBox" Text="{Binding UserName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=Explicit}" Grid.Column="1" Grid.Row="1"></TextBox>
    <dataInput:DescriptionViewer Target="{Binding ElementName=UserNameTextBox}" PropertyPath="Text" Description="{Binding Path=CreateUserResources.UserNameDescription, Source={StaticResource GlobalResources}}" Grid.Column="2" Grid.Row="1"></dataInput:DescriptionViewer>
    

    如果使用MVVM,请确保PropertyPath与演示者模型的属性不匹配。我没有调查,但我有一个例子,我被迫改变我的演示者的属性名,使其工作。似乎控件试图从演示者的属性而不是目标控件分析元数据。

    希望能帮上忙。