-1

Is it possible updating the parent border with triggers from the TextBox is Valid? I tried with Using a Data Template approach. I can not use it because I am trying to pass two properties in data context. One for placeholder and another to bind the User.Email.

If exists a better way to accomplish that achievement, I started using WPF. I also tried using this approach. Created a ControlTemplate and use ScrollViewer x:Name="PART_ContentHost" but the styles don't update.

The point is how a can keep the binding and update the parent border in the more efficient way?

In View:

<Border Style="{DynamicResource TextBoxBorderStyle}" >
    <Grid>
        <TextBlock Text="{Binding Path=UserPlaceholder}"
                                   Style="{StaticResource PlaceHolderTextStyle}">
            <TextBlock.Visibility>
                <MultiBinding Converter="{StaticResource textInputToVisibilityConverter}">
                    <Binding ElementName="Email" Path="Text.IsEmpty" />
                    <Binding ElementName="Email" Path="IsFocused" />
                </MultiBinding>
            </TextBlock.Visibility>
        </TextBlock>
        <TextBox Name="Email" 
            Background="Transparent" 
            Style="{StaticResource textBoxBase}"
            Text="{Binding Path=UserCredentials.Email,
            Mode=TwoWay,
            TargetNullValue='',
            ValidatesOnDataErrors=True,
            UpdateSourceTrigger=PropertyChanged}">
        </TextBox>
    </Grid>
</Border>

In App Border Style:

<Style x:Key="TextBoxBorderStyle"
        TargetType="{x:Type Border}">
    <Setter Property="BorderBrush"
            Value="{DynamicResource TextBoxBorderBrush}"/>
    <Setter Property="Background"
            Value="{StaticResource TextBoxBackgroundBrush}"/>
    <Setter Property="HorizontalAlignment"
            Value="Stretch"/>
    <Setter Property="VerticalAlignment"
            Value="Center"/>
    <Setter Property="BorderThickness"
            Value="2"/>
    <Setter Property="CornerRadius"
            Value="{StaticResource DefaultBorder}"/>
    <Setter Property="Height"
            Value="50"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TextBox}}, Path=Validation.HasError}" Value="True">
            <Setter Property="BorderBrush"
                Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

In App Textblock Style:

<Style x:Key="textBoxBase" TargetType="{x:Type TextBox}">
    <Setter Property="HorizontalAlignment"
            Value="Stretch"/>
    <Setter Property="VerticalAlignment"
            Value="Center"/>
    <Setter Property="Foreground"
            Value="{StaticResource TextBoxForegroundBrush}"/>
    <Setter Property="FontSize"
            Value="16"/>
    <Setter Property="Margin"
            Value="{StaticResource DefaultMargin}"/>
    <Setter Property="FontFamily"
            Value="{StaticResource DefaultFontFamily}"/>
    <Setter Property="BorderThickness"
            Value="0"/>
</Style>
FortyTwo
  • 2,414
  • 3
  • 22
  • 33
F. Santos
  • 29
  • 1
  • 7
  • You use `RelativeSource={RelativeSource AncestorType={x:Type TextBox}}` in the border trigger. Ancestor is not descendant. `TextBox` is placed inside the `Border` so why do you search for `TextBox` using `AncestorType`? – Maxim May 27 '17 at 03:29

1 Answers1

0

To display some elements on error in a control you need to use Validation.ErrorTemplate attached property. You may find this post useful or look at the error template in the MahApps.Metro.

Maxim
  • 1,995
  • 1
  • 19
  • 24