1

I have a TextBox that implements validation to a single property like this:

<TextBox Name="textbox_validation">
    <TextBox.Text>
        <Binding Path="A">
            <Binding.ValidationRules>
                <ExceptionValidationRule/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

With the property like this

private double a;
public double A
{
    get { return a; }
    set
    {
        if (a != value)
        {
            if (value > 22 || value < 10)
            {
                throw new ApplicationException("Invalid value");
            }
            a = value;
            OnPropertyChanged("A");
        }
    }
}

This gives me a nice textbox which will have an indication (red lines) around it when the number is >22 and <10. Lovely.

My problem comes when I have a textbox bound to a class like this:

public class Derp:INotifyPropertyChanged
{
    public Derp(double first, double second, bool third)
    {
        if (!third)
        {
                A = first;
                C = first - (second / 2);
        }
        else
        {
                C = first;
                A = first + second - first; //Please ignore all logic...
        }
    }

    private double c
    public double C
    {
        get { return c; }
        set
        {
                if (c != value)
                {
                    if (value < a)
                    {
                            throw new ApplicationException("Invalid value");
                    }
                    c = value;
                    OnPropertyChanged("C");
                }
        }
    }

    private double a
    public double A
    {
        get { return a; }
        set
        {
                if (a != value)
                {
                    if (value > c)
                    {
                            throw new ApplicationException("Invalid value");
                    }
                    a = value;
                    OnPropertyChanged("A");
                }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

And my textbox can be bound to either A or C depending on the state of my checkbox_next.

<TextBox Grid.Row="0" Grid.Column="1" Name="textbox_differentProperties">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding DerpProperty.A}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=checkbox_next}" Value="True">
                    <Setter Property="Text" Value="{Binding DerpProperty.C}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

I'd like to implement validation for textbox_differentProperties using the method for textbox_validation, thinking along the lines of

<TextBox Grid.Row="0" Grid.Column="1" Name="textbox_differentProperties">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding A}"/>
            <SOME VALIDATION???/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=checkbox_next}" Value="True">
                    <Setter Property="Text" Value={Binding C}/>
                    <VALIDATIONNN???/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Any leads on how to implement validation with datatrigger (changed property) would be very helpful.

5
  • Is there a specific reason you don't want to validate in the ViewModel?
    – kidshaw
    Jul 29, 2014 at 9:45
  • There's validation done in the viewmodel, "value > c" and "value < a", where the error throws. Jul 29, 2014 at 9:49
  • 1
    I should have been clearer, I was wondering if you could avoid XAML triggers. My understanding of your problem is that you want the validation based upon the property that it is bound too - so you could use the INotifyDataErrorInfo interface? This works like INotifyPropertyChanged but notifies of properties in error
    – kidshaw
    Jul 29, 2014 at 9:52
  • Coooooooool. I don't know how it works. I'll check out the documentation. But if you have an example it'll be great too :) Jul 29, 2014 at 9:55
  • I've just searched for one I knew I'd written but can't find it. If I find it later - I'll post it.
    – kidshaw
    Jul 29, 2014 at 10:10

1 Answer 1

2

here you go

note that I have expanded the Value property of Setter, this how we can write Property element syntax in xaml

<TextBox Grid.Row="0"
         Grid.Column="1"
         Name="textbox_differentProperties">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text">
                <Setter.Value>
                    <Binding Path="A">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=checkbox_next}"
                             Value="True">
                    <Setter Property="Text">
                        <Setter.Value>
                            <Binding Path="C">
                                <Binding.ValidationRules>
                                    <ExceptionValidationRule />
                                </Binding.ValidationRules>
                            </Binding>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
1
  • omg. xD this looks so simple. I should've gotten it. Shall test then accept. Jul 29, 2014 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.