none
AdornedElementPlaceholder

Risposte

  • Hi Damien,

    AdornedElementPlaceholder is part of the Validation feature of data binding. Validation is used to alert the user that the data he entered is illegal. For example, in the scenario below the data source is of type int, so if the user types something that can not be converted to int, we will generate a Validation error. Notice that you can write your own custom validation rules that determine the format of the data allowed.

    If you don't set the Validation.ErrorTemplate dependency property, by default we show a red rectangle around the control in error, when there is a Validation error. We wanted to allow maximum flexibility for the developer to control the UI displayed when there are errors. This can be done by setting the Validation.ErrorTemplate DP to a ControlTemplate where you can add any visual tree you want. AdornedElementPlaceholder should be used in this visual tree to reserve space for the control in error. For example, in the markup below, I am displaying a ! to the left of the TextBox when there is a validation error.

     <ControlTemplate x:Key="validationTemplate">
      <StackPanel Orientation="Horizontal">
       <TextBlock Foreground="Red" FontSize="22" FontWeight="Bold" Margin="0,0,5,0">!</TextBlock>
       <AdornedElementPlaceholder />
      </StackPanel>
     </ControlTemplate>

     <TextBox Width="150" Margin="10" Validation.ErrorTemplate="{StaticResource validationTemplate}">
      <TextBox.Text>
       <Binding Source="{StaticResource source}" Path="Prop" UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
         <ExceptionValidationRule />
        </Binding.ValidationRules>
       </Binding>
      </TextBox.Text>
     </TextBox>

    You can find a VS project with this working here: http://www.beacosta.com/Forum/AdornedElementPlaceholderSample.zip.

    Let me know if this is clear.

    Bea

    lunedì 6 marzo 2006 05:55
    Moderatore

Tutte le risposte

  • Hi Damien,

    AdornedElementPlaceholder is part of the Validation feature of data binding. Validation is used to alert the user that the data he entered is illegal. For example, in the scenario below the data source is of type int, so if the user types something that can not be converted to int, we will generate a Validation error. Notice that you can write your own custom validation rules that determine the format of the data allowed.

    If you don't set the Validation.ErrorTemplate dependency property, by default we show a red rectangle around the control in error, when there is a Validation error. We wanted to allow maximum flexibility for the developer to control the UI displayed when there are errors. This can be done by setting the Validation.ErrorTemplate DP to a ControlTemplate where you can add any visual tree you want. AdornedElementPlaceholder should be used in this visual tree to reserve space for the control in error. For example, in the markup below, I am displaying a ! to the left of the TextBox when there is a validation error.

     <ControlTemplate x:Key="validationTemplate">
      <StackPanel Orientation="Horizontal">
       <TextBlock Foreground="Red" FontSize="22" FontWeight="Bold" Margin="0,0,5,0">!</TextBlock>
       <AdornedElementPlaceholder />
      </StackPanel>
     </ControlTemplate>

     <TextBox Width="150" Margin="10" Validation.ErrorTemplate="{StaticResource validationTemplate}">
      <TextBox.Text>
       <Binding Source="{StaticResource source}" Path="Prop" UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
         <ExceptionValidationRule />
        </Binding.ValidationRules>
       </Binding>
      </TextBox.Text>
     </TextBox>

    You can find a VS project with this working here: http://www.beacosta.com/Forum/AdornedElementPlaceholderSample.zip.

    Let me know if this is clear.

    Bea

    lunedì 6 marzo 2006 05:55
    Moderatore
  • Bea,

    Whatever I do I cannot rotate AdornerElementPlaceholder. Why?

        <ControlTemplate x:Key="validationTemplate">
          <Border BorderBrush="Red" BorderThickness="2">
            <Border.RenderTransform>
              <RotateTransform CenterX="0" CenterY="0" Angle="10"/>
            </Border.RenderTransform>
            <AdornedElementPlaceholder>
              <AdornedElementPlaceholder.RenderTransform>
                <RotateTransform CenterX="0" CenterY="0" Angle="20"/>
              </AdornedElementPlaceholder.RenderTransform>
            </AdornedElementPlaceholder>
          </Border>
        </ControlTemplate>

    Alex

    giovedì 9 marzo 2006 21:28
  • Alex,

    The markup your showed is rotating the AdornedElementPlaceholder. If you remove the RenderTransform in the Border and leave the one in the AdornedElementPlaceholder, the border appears rotated when the TextBox is in error.

       <ControlTemplate x:Key="validationTemplate">
          <Border BorderBrush="Red" BorderThickness="2">
            <AdornedElementPlaceholder>
              <AdornedElementPlaceholder.RenderTransform>
                <RotateTransform CenterX="0" CenterY="0" Angle="20"/>
              </AdornedElementPlaceholder.RenderTransform>
            </AdornedElementPlaceholder>
          </Border>
        </ControlTemplate>

    Notice that the TextBox does not get placed in the AdornedElementPlaceholder. The AEP is simply an empty area in the adorner layer that is the same size of the TextBox. If you rotate it, only the elements around that empty area will rotate (in this case, the Border).

    Why is it in a different layer? Because this way you can use cool visual effects on top of your TextBox when it is in error (try to put something inside the AEP to see what I mean).

    I'm assuming that you are trying to rotate the TextBox (and adorner around it) when the data entered is not valid, is this true? If so, you can attach an error event handler for the Validation and rotate the TextBox there. You don't need to rotate the AdornerElementPlaceholder in this case, since it asks the TextBox what space it should leave empty.

            <ControlTemplate x:Key="validationTemplate">
                <Border BorderBrush="Red" BorderThickness="2">
                    <AdornedElementPlaceholder />
                </Border>
            </ControlTemplate>

            <TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}" Margin="20" Name="tb">
                <TextBox.Text>
                    <Binding Source="{StaticResource source}" Path="Prop" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
                        <Binding.ValidationRules>
                            <local:RangeRule Min="0" Max="10" ErrorContent="must lie between 0 and 10" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

            protected override void OnInitialized(EventArgs e)
            {
                base.OnInitialized(e);
                Validation.AddErrorHandler(tb, new EventHandler<ValidationErrorEventArgs>(InError));
            }

            private void InError(object sender, ValidationErrorEventArgs args)
            {
                TextBox tb = (TextBox)sender;
                tb.RenderTransform = new RotateTransform(20);
            }

    You can find a complete sample with this code in http://www.beacosta.com/Forum/RotatePlaceholder.zip.

    Let me know if this is what you wanted to know.

    Bea

     

    venerdì 10 marzo 2006 00:06
    Moderatore
  • Beatriz,

    That's exactly what I was trying to do.

    Thank you very much.

    Alex

    venerdì 10 marzo 2006 03:25