开发者

WPF TextBox separate Validation source

开发者 https://www.devze.com 2023-01-06 10:01 出处:网络
I am trying to find a solution to let a TextBox show and validate data from two different sources as this little example shows:

I am trying to find a solution to let a TextBox show and validate data from two different sources as this little example shows:

<TextBox Text="{Binding Port.Name}" 
ValidationSource="{Binding Ship.PortFK}"/>

Here the Ship data object contains a property that functions as a Foreign Key to a Port, the property has its own validators.

I have explored the use of ValidationRules of the link below, but this only gives me the value of PortFK while i need the property with its validation atributes.

http://michlg.wordpress.com/2010/01/29/wpf-custom-validationrule-with-an-additional-parameter/

Does anyone know a solution to this?

-- edit --

I see that i did not explain my problem well enough ;).

Actually the ValidationSource tag does not exist within .Net or my own program as i don't know how how to create such a construction that takes a binding to a property, validates the property using its validation attributes and somehow provides the validation information to the TextBox's validation structure.

To further elaborate the data objects:

public class Port
{
    public int PortPK { get; set; }

    [StringLengthValidator]
    public string Name { get; set; }

    ...
}

public class Ship
{
    [NotNullValidator]
    public int PortFK { get; set; }

    ...
}

So the TextBox.Text binding should show the Port.Name without validating it. The validation should be done on Ship.PortFK which will show whether or not a Port has been set to the Ship.

-- edit 2 --

The form is a CRUD form for editing Ship data. The Textbox is part of a selector control that consists of this TextBox that shows Port.Name and a button used to select a specific Port.

The validation should indicate whether or not a Port has been selected for the Ship. For this the NotNullValidator is used on Ship.PortFK, if the PortFK value has not been set the validator is fired and the user is warned to select a Port.

Thus the user can select a Specific Port for a Ship, after which the TextBox will show the Port.Name. As the datamodel defines that a Port is obligatory for a Ship, the validation of the TextBox should indicate tot the user whether or n开发者_运维百科ot a Port has yet been selected for the Ship.


Could you make use of a MultiBinding? A MultiBinding allows you to translate multiple values into a single value.

<TextBox.ValidationSource>
  <MultiBinding Converter="{StaticResource multiValueConverter}">
    <Binding Path="Ship.Prop1" />
    <Binding Path="Ship.Prop2" />
  </MultiBinding>
</TextBox.ValidationSource>

Above, the two properties of Ship are passed to the IMultiValueConverter named multiValueConverter (you'll need to implement your own converter).

Your IMultiValueConverter implementation should convert the values of those two properties to a single value which can be consumed by whatever you're binding to - there's no getting around that to my knowledge - but that single value could be an object[].

If this doesn't help, maybe you could provide us with some more source code and the structures of the objects you're binding to?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号