2010-01-06 2 views
1

Silverlight в DataInput: Этикетка и DataInput: DescriptionViewer управления обеспечивают полезную функцию благодаря их способности связываться с TextBox. Вместо того, чтобы жестко кодировать текстовое содержимое моих меток и описаний с помощью System.ComponentModel.DataAnnotations.DisplayAttribute, я бы предпочел привязать эти атрибуты к свойствам класса, содержащего текст, редактируемый администратором. Это работает для Label, но не для DescriptionViewer.Связывание Silverlight 3 Описание атрибут DataInput: DescriptionViewer

В частности, это метка работы:

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

но DescriptionViewer невидим:

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

Когда я удалить связывание с моей TextBox, то DescriptionViewer показано:

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

Должно ли быть возможно привязывать DescriptionViewer как к TextBox, так и к альтернативному описанию источник ription?

Заранее благодарен!

MainPage.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> 

MainPage.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 

DataClass.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

У меня была точно такая же проблема с 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> 

Перед привязкой Описание к его ресурсу это была простая строка в разметке, и это действительно сработало.

Чтобы решить эту проблему, я установил PropertyPath of DescriptionViewer с именем свойства, где привязка находится в элементе управления Target. Теперь он работает правильно.

<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 не соответствует свойству вашей модели презентатора. Я не исследовал, но у меня был экземпляр, где я был вынужден изменить имя свойства моего ведущего, чтобы он работал. Кажется, что элемент управления пытался проанализировать метаданные из свойства моего ведущего вместо целевого элемента управления.

Надеюсь, что эта помощь.

Смежные вопросы