mscommunity.net

Interactive mscommunity.net online activities
Signed in as anonymous | Edit Profile | Sign out | Help
in Search

Weblog :: Boris Ševo

Sporadic posts about my interests, e.g. software development (mostly .NET), technology in general and some occasional rant.

veljača 2009 - Posts

  • WPF trick #4 - Binding and Validation

    Validation can also happen while binding is made. This is very useful if the bound objects have built in logic. For example if you have a Product object which has Price property which must be some number bigger the zero you will probably have code similar to this one:

    class Product
    {
        private decimal _price;

        public decimal Price
        {
            get { return _price; }
            set
            {
                if (value <= 0)
                    throw new Exception("Exception from Price setter");
                _price = value;
            }
    }

    If you need to mark invalid input inside some TextBox you can then write something like this:

        <Window.Resources>
    <code:Product x:Key="dsProduct"/>
    </Window.Resources>
    <StackPanel>
    <WrapPanel>
    <TextBox Width="200" Text="{Binding
    Source={StaticResource dsProduct},
    Path=Price,
    UpdateSourceTrigger=PropertyChanged,
    ValidatesOnDataErrors=True,
    ValidatesOnExceptions=True}">
    </TextBox>
    </WrapPanel>
    </StackPanel>
    Posted vlj 05 2009, 10:35 by boris.sevo with no comments
    Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems