×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

WPF

本文发表在 rolia.net 枫下论坛<Window x:Class="BindValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:BindValidation"
Title="Binding Validation Sample"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize">

<Window.Resources>
<c:MyDataSource x:Key="ods"/>

<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>

<TextBlock
Grid.Row="0" Grid.ColumnSpan="2"
FontSize="20" Margin="8"
Text="Enter a number between 21-130 or there will be a validation error:"/>

<Label Grid.Column="0" Grid.Row="1" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox1}">TextBox with _custom ErrorTemplate and ToolTip:</Label>
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Grid.Row="2" Grid.Column="0" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox2}">TextBox with _default ErrorTemplate:</Label>
<TextBox Name="textBox2" Width="50" FontSize="15"
Grid.Row="2" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age2" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

<TextBlock Grid.Row="3" Grid.ColumnSpan="3" FontSize="20" Margin="8"
Text="The following TextBox uses the ExceptionValidationRule and UpdateSourceExceptionFilter handler:"/>
<TextBlock Grid.Row="4" Grid.ColumnSpan="3" FontSize="15" Margin="8"
Text="Enter a non-numeric value to raise the exception."/>
<Label Grid.Row="5" Grid.Column="0" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox3}">TextBox with UpdateSourceExceptionFilter _handler:</Label>

<TextBox Name="textBox3" Width="50" FontSize="15"
Grid.Row="5" Grid.Column="1" Margin="2"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="Age3" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<CheckBox Name="cb" FontSize="15" HorizontalAlignment="Left"
Grid.Row="5" Grid.Column="2" Margin="5" IsChecked="True"
Checked="UseCustomHandler" Unchecked="DisableCustomHandler">Enable Custom Handler (see ToolTip)</CheckBox>
</Grid>
</Window>更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 也别说C#/VB.NET谁好了,现在的.NET,觉得搞脑子的还是这些。
    • WPF
      本文发表在 rolia.net 枫下论坛<Window x:Class="BindValidation.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:c="clr-namespace:BindValidation"
      Title="Binding Validation Sample"
      SizeToContent="WidthAndHeight"
      ResizeMode="NoResize">

      <Window.Resources>
      <c:MyDataSource x:Key="ods"/>

      <ControlTemplate x:Key="validationTemplate">
      <DockPanel>
      <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
      <AdornedElementPlaceholder/>
      </DockPanel>
      </ControlTemplate>
      <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
      <Style.Triggers>
      <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
      Value="{Binding RelativeSource={x:Static RelativeSource.Self},
      Path=(Validation.Errors)[0].ErrorContent}"/>
      </Trigger>
      </Style.Triggers>
      </Style>
      </Window.Resources>

      <Grid>
      <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition/>
      <RowDefinition />
      </Grid.RowDefinitions>

      <TextBlock
      Grid.Row="0" Grid.ColumnSpan="2"
      FontSize="20" Margin="8"
      Text="Enter a number between 21-130 or there will be a validation error:"/>

      <Label Grid.Column="0" Grid.Row="1" FontSize="15" Margin="2"
      Target="{Binding ElementName=textBox1}">TextBox with _custom ErrorTemplate and ToolTip:</Label>
      <TextBox Name="textBox1" Width="50" FontSize="15"
      Validation.ErrorTemplate="{StaticResource validationTemplate}"
      Style="{StaticResource textBoxInError}"
      Grid.Row="1" Grid.Column="1" Margin="2">
      <TextBox.Text>
      <Binding Path="Age" Source="{StaticResource ods}"
      UpdateSourceTrigger="PropertyChanged" >
      <Binding.ValidationRules>
      <c:AgeRangeRule Min="21" Max="130"/>
      </Binding.ValidationRules>
      </Binding>
      </TextBox.Text>
      </TextBox>
      <Label Grid.Row="2" Grid.Column="0" FontSize="15" Margin="2"
      Target="{Binding ElementName=textBox2}">TextBox with _default ErrorTemplate:</Label>
      <TextBox Name="textBox2" Width="50" FontSize="15"
      Grid.Row="2" Grid.Column="1" Margin="2">
      <TextBox.Text>
      <Binding Path="Age2" Source="{StaticResource ods}"
      UpdateSourceTrigger="PropertyChanged" >
      <Binding.ValidationRules>
      <c:AgeRangeRule Min="21" Max="130"/>
      </Binding.ValidationRules>
      </Binding>
      </TextBox.Text>
      </TextBox>

      <TextBlock Grid.Row="3" Grid.ColumnSpan="3" FontSize="20" Margin="8"
      Text="The following TextBox uses the ExceptionValidationRule and UpdateSourceExceptionFilter handler:"/>
      <TextBlock Grid.Row="4" Grid.ColumnSpan="3" FontSize="15" Margin="8"
      Text="Enter a non-numeric value to raise the exception."/>
      <Label Grid.Row="5" Grid.Column="0" FontSize="15" Margin="2"
      Target="{Binding ElementName=textBox3}">TextBox with UpdateSourceExceptionFilter _handler:</Label>

      <TextBox Name="textBox3" Width="50" FontSize="15"
      Grid.Row="5" Grid.Column="1" Margin="2"
      Validation.ErrorTemplate="{StaticResource validationTemplate}"
      Style="{StaticResource textBoxInError}">
      <TextBox.Text>
      <Binding Path="Age3" Source="{StaticResource ods}"
      UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
      <ExceptionValidationRule/>
      </Binding.ValidationRules>
      </Binding>
      </TextBox.Text>
      </TextBox>
      <CheckBox Name="cb" FontSize="15" HorizontalAlignment="Left"
      Grid.Row="5" Grid.Column="2" Margin="5" IsChecked="True"
      Checked="UseCustomHandler" Unchecked="DisableCustomHandler">Enable Custom Handler (see ToolTip)</CheckBox>
      </Grid>
      </Window>更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • 叫上劲啦
        • 主要是无聊。
          • 一只大猫, 快出来应战哪
            • 起哄。我跟大猫是阶级兄弟,没我抬抬杠就显无趣。
            • 应 what 战 ?
              • VB.NET
                • 这叫友好的阶级讨论,都是闲出来的。要说应战,我现在正在国内论坛和一帮像你一样支持巴勒、东突的毛左小将应战
                  • 真荣幸。证据呢?在家纳大污蔑是要负责任地.
                    ha ha ha
                    • 荣幸就好,大周末的,证据吗!给你你也看不懂,就算了,信不信由你啦
                      • 那肯定是阿拉伯文吧
                        • 不是啊,您继续猜。对了,以前那个疯狂支持恐怖份子的台湾ax大姐是不是你的马甲啊?
                          • 水是AX呀?西拨赖文吧?
                            • 好啦好啦,不拿你开心了,我用的是火星文,所以说给你你也看不懂,你还一个劲问。回家了,大周末的。您继续自娱自乐,白白
                              • 多抓几只老鼠或是冻土分子什末的。败败!
                          • AX 好象是Armani Exchange
                  • 啊猫真忙。家国中国的论潭上都大显身手
        • 大家就大闲聊灌水嘛,别当真。其实干活用啥领导说了算,没有个人喜好的自由。
          • 同意
      • 唉,在 gta,这个再熟,还不如随便会点儿 asp.net / javascript 好找工作...
    • WCF
      本文发表在 rolia.net 枫下论坛<?xml version="1.0"?>
      <configuration>
      <system.serviceModel>
      <client>
      <endpoint name="EchoServer"
      address="http://localhost:8000/TestService/BinaryEncoderOverHTTP"
      binding="netHttpBinding"
      bindingConfiguration="netHttpBinding"
      contract="Microsoft.Samples.NetHttpBinding.IEchoService">
      <!--Include the identity element when running cross-machine in a domain.-->
      <!--
      <identity>
      <userPrincipalName value="user_name@service_domain"/>
      </identity>
      -->
      </endpoint>
      </client>


      <behaviors>
      <endpointBehaviors>
      <behavior name="securityBehavior">
      <clientCredentials>
      <clientCertificate storeLocation="LocalMachine"
      storeName="My"
      x509FindType="FindBySubjectName"
      findValue=""/>
      <windows allowNtlm="false"
      allowedImpersonationLevel="Impersonation"/>
      </clientCredentials>
      </behavior>
      </endpointBehaviors>
      </behaviors>


      <bindings> <!-- BindingsSectionn-->
      <netHttpBinding> <!-- Binding collection element: NetHttpBindingCollectionElement -->
      <binding name="netHttpBinding"
      useDefaultWebProxy="true"
      hostNameComparisonMode="StrongWildcard"
      securityMode="TransportCredentialOnly">

      </binding> <!--NetHttpBindingElement-->
      <!-- Class NetHttpBinding
      NetHttpBindingElement.ApplyConfiguration(NetHttpBinding);
      -->
      </netHttpBinding>
      </bindings>
      <extensions>
      <bindingExtensions>
      <add name="netHttpBinding"
      type="Microsoft.Samples.NetHttpBinding.NetHttpBindingCollectionElement, NetHttpBinding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </bindingExtensions>
      </extensions>
      </system.serviceModel>
      </configuration>更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 什么叫搞脑子?最头痛的应该是 F#,写起来很漂亮很爽,可是一旦有问题, Debug 那个才叫烦,因为所有语句就一行,watch or immediate window doesn't support lamda,结果要 debug 还要先翻译成一句一句的,我晕
      • 没用过,刚点开来看看VS2010里没几个F#的程序模板嘛,debug功能差那很头痛。
      • 同感,C#里面Lamda表达式用多了如果出现错误,调式也是个很头疼的问题,和你一样,我也要把表达式拆分成一个一个独立的小函数查找错误。不过听说VS 2010出现了一个什么新东西可以解决这个困难,我还没仔细看