Silverlight中的数据验证具有多个对象

时间:2012-07-01 11:52:40

标签: c# data-binding validation silverlight-5.0

我的Silverlight表单需要为3个类的对象中的字段输入值,这些字段将共同调用Web服务的请求

GUI的代码正在进行中

<UserControl x:Class="ClientSanitaro.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"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:my="clr-namespace:It.Unina.MasterICT.ClientSanitario.Controls" xmlns:data="clr-namespace:It.Unina.MasterICT.ClientSanitario.Data"  Loaded="UserControl_Loaded">


<sdk:TabControl Height="400" HorizontalAlignment="Center" Margin="10,10,0,0" Name="tabControl" VerticalAlignment="Top" Width="550">        
    <sdk:TabItem Header="Upload documenti" Name="tabUpload">
        <sdk:TabItem.DataContext>
            <data:PazienteGui/>
        </sdk:TabItem.DataContext>


        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.Resources>
                    <Style TargetType="TextBox">
                        <Setter Property="Margin" Value="2"/>
                    </Style>
                    <Style TargetType="sdk:Label">
                        <Setter Property="Margin" Value="2"/>
                    </Style>
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3*"/>
                    <ColumnDefinition Width="5*"/>
                </Grid.ColumnDefinitions>
                <TextBox Height="23" HorizontalAlignment="Left" Name="txtUploadEndpoint" Width="300" Grid.Column="1" VerticalAlignment="Center" />
                <sdk:Label Height="23" HorizontalAlignment="Right" Name="lblUploadEndpoint" VerticalAlignment="Center" Width="80" Content="URL endpoint" Grid.Column="0" />
            </Grid>

            <Grid Grid.Row="1" Margin="15">
                <Grid.Resources>
                    <Style TargetType="TextBox">
                        <Setter Property="Margin" Value="3"/>
                    </Style>
                    <Style TargetType="sdk:Label">
                        <Setter Property="Margin" Value="3"/>
                    </Style>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>
                <sdk:Label Content="Persona" FontWeight="Bold" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
                <sdk:Label Content="Cognome" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"/>
                <TextBox Name="txtPersonaCognome" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch"/>
                <sdk:Label Content="Nome" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"/>
                <TextBox Name="txtNome" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch"/>
                <sdk:Label Content="Codice Fiscale" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" />
                <TextBox Name="txtPersonaCodiceFiscale" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Stretch"/>
                <sdk:Label Content="Residenza" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right"/>
                <TextBox Name="txtPazienteResidenza" Grid.Row="4" HorizontalAlignment="Stretch" Grid.Column="1"/>
                <sdk:Label Content="Struttura Sanitaria" Grid.Row="5" Grid.Column="0" HorizontalAlignment="Left" Grid.ColumnSpan="2" FontWeight="Bold" />
                <sdk:Label Content="Nome" Grid.Row="6" Grid.Column="0" HorizontalAlignment="Right"/>
                <TextBox Name="txtStrutturaNome" Grid.Row="6" Grid.Column="1" HorizontalAlignment="Stretch"/>
                <sdk:Label Content="Indirizzo" Grid.Row="7" Grid.Column="0" HorizontalAlignment="Right"/>
                <TextBox Name="txtStrutturaIndirizzo" Grid.Row="7" Grid.Column="1" HorizontalAlignment="Stretch"/>
                <sdk:Label Content="Documento Sanitario" Grid.Row="0" Grid.Column="2" FontWeight="Bold" HorizontalAlignment="Left" Grid.ColumnSpan="2"/>
                <sdk:Label Content="Contenuto" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right"/>
                <ComboBox Name="cmbTipoContenuto" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Stretch"/>
                <sdk:Label Content="Tipo MIME" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Right"/>
                <TextBlock Name="lblMimeType" Grid.Row="2" Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>

                <sdk:Label Content="File" Grid.Row="4" Grid.Column="2" HorizontalAlignment="Right"/>
                <my:FileUpload Grid.Column="3" Grid.Row="4" Grid.RowSpan="2" HorizontalAlignment="Stretch" x:Name="fileUpload" VerticalAlignment="Top" FileUploaded="fileUpload_FileUploaded" FileRemoved="fileUpload_FileRemoved" />
                <Button Content="Upload" Grid.Column="3" Grid.Row="7" HorizontalAlignment="Stretch" Name="btnUpload" VerticalAlignment="Stretch" Click="btnUpload_Click" />
            </Grid>
        </Grid>

    </sdk:TabItem>
    <sdk:TabItem Header="Ricerca documenti" Name="tabRicerca">
        <Grid></Grid>
    </sdk:TabItem>
</sdk:TabControl>

如你所见,在网格中我有几个字段,都是必需的(txtCodiceFiscale是16个字母数字字符长)。我的服务参考定义了3个主要类:Persona(人)Documento(文档)和StrutturaSanitaria(HealthFacility)。阅读我发现执行验证的“更好”方式是使用数据绑定(我可以在经典的WinForms中轻松完成),也许它与MVVM模式有关,我还没有掌握。< / p>

我了解到,不是让表单按钮的Click事件手动验证代码,而是以传统方式将表单及其文本框绑定到数据上下文对象的属性。

问题

我需要3个数据对象。到目前为止我找到的所有示例都只显示了一个分配给根控件的对象(在我的情况下,我需要将对象绑定到第一个TabItem,因为第二个选项卡应该显示来自Web服务的结果,并且将被合理地绑定)。是否可以将控件绑定到多个对象? (我不知道指定多个数据对象的语法)如果没有,我至少可以将它绑定到这样的类并引用树中的每个属性吗?

public class DataContainer {
    public Persona Persona{get; set;}
    public Documento Documento {get; set;}
    public StrutturaSanitaria Struttura {get; set;}
}

这让我省去了定义一个体现所有值的类的麻烦,所以至少当我点击Submit时,我所有的对象都充满了数据。

或者,您是否有其他可行的解决方案,代码便宜?我正试图找一些书,但我没有时间在截止日期之前阅读。有人能给我一个关于数据验证的好教程,帮助我理解背后的机制,或者简单地向我解释一下吗?

1 个答案:

答案 0 :(得分:1)

Colin Eberhardt几年前在Silverlight上发表了一篇关于multiple binding的博客文章。也许这会有所帮助吗?

这基本上与WPF MultiBinding类中应用的方法相同。