Natalie Tay
Natalie Tay

Reputation: 133

WPF TextBox binding to different properties with ValidationRule

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.

Upvotes: 1

Views: 2367

Answers (1)

pushpraj
pushpraj

Reputation: 13669

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>

Upvotes: 2

Related Questions