Texbox with MarginValidationRule

MarginValidationRule



The Code
<window focusmanager.focusedelement="{Binding ElementName=leftMarginTextBox}" height="210" minheight="10" minwidth="300" resizemode="CanResizeWithGrip" showintaskbar="False" title="Margins" width="300" windowstartuplocation="CenterOwner" x:class="DialogBoxSample.MarginsDialogBox" xmlns:local="clr-namespace:DialogBoxSample" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

  <grid>
    <grid.resources>
      <style targettype="{x:Type Grid}">
        <setter Property="Margin" Value="10" />
      
</style>
      <style targettype="{x:Type Label}">
        <setter Property="Margin" Value="0,3,5,5" />
        <setter Property="Padding" Value="0,0,0,5" />
      
</style>
      <style targettype="{x:Type TextBox}">
        <setter Property="Margin" Value="0,0,0,5" />
        <Style.Triggers>
          <trigger Property="Validation.HasError" Value="true">
            <setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
          </Trigger>
        </Style.Triggers>
      
</style>
      <style targettype="{x:Type StackPanel}">
        <setter Property="Orientation" Value="Horizontal" />
        <setter Property="HorizontalAlignment" Value="Right" />
      
</style>
      <style targettype="{x:Type Button}">
        <setter Property="Width" Value="70" />
        <setter Property="Height" Value="25" />
        <setter Property="Margin" Value="5,0,0,0" />
      
</style>
    </grid.resources>

    <grid.columndefinitions>
      <columndefinition width="Auto">
      <columndefinition>
    </columndefinition></columndefinition></grid.columndefinitions>

    <grid.rowdefinitions>
      <rowdefinition height="Auto">
      <rowdefinition height="Auto">
      <rowdefinition height="Auto">
      <rowdefinition height="Auto">
      <rowdefinition height="Auto">
      <rowdefinition>
    </rowdefinition></rowdefinition></rowdefinition></rowdefinition></rowdefinition></rowdefinition></grid.rowdefinitions>

    
    <label grid.column="0" grid.row="0">Left Margin:</label>
    <textbox grid.column="1" grid.row="0" name="leftMarginTextBox">
      <textbox.text>
        <binding path="Left" updatesourcetrigger="PropertyChanged">
          <binding.validationrules>
            <local:marginvalidationrule maxmargin="100" minmargin="0">
          </local:marginvalidationrule></binding.validationrules>
        </binding>
      </textbox.text>
    </textbox>

    
    <label grid.column="0" grid.row="1">Top Margin:</label>
    <textbox grid.column="1" grid.row="1" name="topMarginTextBox">
      <textbox.text>
        <binding path="Top" updatesourcetrigger="PropertyChanged">
          <binding.validationrules>
            <local:marginvalidationrule maxmargin="10" minmargin="0">
          </local:marginvalidationrule></binding.validationrules>
        </binding>
      </textbox.text>
    </textbox>

    
    <label grid.column="0" grid.row="2">Right Margin:</label>
    <textbox grid.column="1" grid.row="2" name="rightMarginTextBox">
      <textbox.text>
        <binding path="Right" updatesourcetrigger="PropertyChanged">
          <binding.validationrules>
            <local:marginvalidationrule maxmargin="10" minmargin="0">
          </local:marginvalidationrule></binding.validationrules>
        </binding>
      </textbox.text>
    </textbox>

    
    <label grid.column="0" grid.row="3">Bottom Margin:</label>
    <textbox grid.column="1" grid.row="3" name="bottomMarginTextBox">
      <textbox.text>
        <binding path="Bottom" updatesourcetrigger="PropertyChanged">
          <binding.validationrules>
            <local:marginvalidationrule maxmargin="10" minmargin="0">
          </local:marginvalidationrule></binding.validationrules>
        </binding>
      </textbox.text>
    </textbox>

    
    <stackpanel grid.column="0" grid.columnspan="2" grid.row="4">
      <button click="okButton_Click" isdefault="True" name="okButton">OK</button>
      <button iscancel="True" name="cancelButton">Cancel</button>
    </stackpanel>

  </grid>

</window>
Texbox with MarginValidationRule Texbox with MarginValidationRule Reviewed by Bhaumik Patel on 11:21 PM Rating: 5