0

basiclly the input of the TextBox is an articel number, and if the number is older then 6 Month I want to mark the border of my textbox red. For that I need to get data from an SQL Query btw.

The Code logic is working thats not the problem Ive got.

I tried multiple things, what I had tried at first was with an Textchange event which looks similiar to this:

TextBox tbx = sender as TextBox;

        if (tbx.Text.Length > 7)
        {
            string getDate = SQL
            try
            {
                using (var reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        date = reader.GetDateTime(0);

                    }
                    reader.Close();
                }

                var lastmonth = DateTime.Today.AddMonths(-6);

                if (lastmonth < date)
                {
                    tbx.BorderBrush = (Brush)Application.Current.MainWindow.FindResource("BorderBrushWrong");
                }
                else
                {
                    tbx.BorderBrush = (Brush)Application.Current.MainWindow.FindResource("BorderBrush");
                }


            }
            catch
            {
                
            }

note I removed here the SQL Query stuff but In my Code its there

but with this Variant, after I put in the number the Textbox border getting red, but the selector is freezing and I only cant switch the textbox after 5 clicks or something. And the cursor is still in the textbox and I cant mark the text or do all the function an textbox got. I only can remove the Text from the delete button on the keyboard.

After that didnt work I tried it on the Text Property that is bound to the textbox:

 private string _TopArtikel;
    public string TopArtikel
    {
        get
        {
            //replaceHTMLnew();
            return _TopArtikel;

        }
        set
        {
            _TopArtikel = value;
            inputCheck(_TopArtikel);
            NotifyPropertyChanged(nameof(TopArtikel));
        }
    }

on Input check is the same Code and to change the color I make instead of that:

tbx.BorderBrush = (Brush)Application.Current.MainWindow.FindResource("BorderBrushWrong");

this

colorcheck = true;

thats an bool property I created and in my style of my textbox I made an DataTrigger:

 <DataTrigger Binding="{Binding colorcheck}" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                        </DataTrigger>

but I ran into the Same Issue

the third try was with an Validation rule which I Implemented like this:

  public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        NewsViewModel viewModel = new NewsViewModel();
        string input = value.ToString();
        if (input.Length > 7)
        {


            string getDate = SQL

            DateTime date = new DateTime();

            IGenericCommand command = viewModel.mandant.MainDevice.GenericConnection.CreateSqlStringCommand(getDate);

            command.AppendInParameter("artikelnummer", typeof(string), input);
            try
            {
                using (var reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        date = reader.GetDateTime(0);

                    }
                    reader.Close();
                }

                var lastmonth = DateTime.Today.AddMonths(-6);

                if (lastmonth < date)
                {
                    return new ValidationResult(false,"");
                }
                else
                {
                    return new ValidationResult(true, null);
                }


            }
            catch
            {
                MessageBox.Show("Fehler");
            }
        }
        return new ValidationResult(true, null);
    }

and in xaml this:

<Binding Path="TopArtikel" ValidatesOnExceptions="True"
                        UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <local:MonthValidation/>
                            </Binding.ValidationRules>
                        </Binding>

But with this I got the Issue that the Placeholder Text of my Textbox not removing and the Color not changing. The Input I made is overlapping with the Placeholder Text.

Has some of you an Idea on how to get the Border Red when I made and articel number Input which is older than 6 Month? I already have the Code, I have no Idea how to make this Validation Correctly.

My TextBox Style in case you need this:

        <Style x:Key="txtStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="BorderBrush" Value="#e5e7ea"/>
        <Setter Property="TextWrapping" Value="Wrap"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Border x:Name="border" Grid.ColumnSpan="2" Margin="0 2 0 2 " Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" 
                                CornerRadius="5" BorderThickness="1.5" BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
                        <ScrollViewer Grid.Column="2" x:Name="PART_ContentHost" Margin="5,5,0,5" VerticalAlignment="Center" />
                        <Label Margin="5,0 0,0" HorizontalAlignment="Left" Grid.Column="1" x:Name="WaterMarkLabel" VerticalAlignment="Center"
                       Visibility="Collapsed" Foreground="Gray" FontFamily="Arial" Content="{Binding Tag, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Text" Value=""/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
                        </MultiTrigger>
                        <DataTrigger Binding="{Binding colorcheck}" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                        </DataTrigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="White"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="DimGray"/>
                        </Trigger>
                        <Trigger Property="IsReadOnly" Value="True">
                            <Setter Property="Background" Value="#f0f0f0"/>
                            <Setter Property="BorderThickness" Value="0"/>

                        </Trigger>
                        <EventTrigger 
                        RoutedEvent="TextBox.GotFocus">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="border"    
                                    Storyboard.TargetProperty="BorderBrush.Color"   
                                    To="#5B7FFF" Duration="00:00:00.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger 
                        RoutedEvent="TextBox.LostFocus">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="border"    
                                    Storyboard.TargetProperty="BorderBrush.Color"   
                                    To="#e5e7ea" Duration="00:00:00.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>

        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                    <Condition Property="IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>

0 Answers0