Page view counter
Are Data Binding Details Accessible in Beta 2?
Last post 07-09-2008 8:06 AM by WJamesLord. 6 replies.
Sort Posts:
07-04-2008 10:32 AM
Are Data Binding Details Accessible in Beta 2?

As in Beta1, the only data binding information that I see exposed in Beta2 is the DataContext property for any control.  While this provides the object that was bound to a control, it does provide the property.  Unfortunately, I need the property to cross-reference some meta-data that I intend to utilize to "bind" each bound control to enforce contraints such as maximum length and input format.

Does anyone know if there is a way to access the Binding object used for any control?

Thanks in advance,

W James

WJamesLord

Loading...
Joined on 06-19-2007
Posts 104
07-05-2008 11:38 AM
Re: Re: Are Data Binding Details Accessible in Beta 2?

Thanks for the URLs, Ken.  IValueConverters are a good feature.  I use them quite a bit.  However, the interface doesn't provide the means to reference these source property in the bound data object, which is really what I need.  They are really meant for formatting, handling exceptions, and other special case scenarios.

The scenario that I need to address is slightly different.  At bind time, I want to apply certain attributes to a control based upon meta-data stored in my custom data object that I use to serialize/deserialize values to/from service providers and Silverlight client apps.  The IValueConverter interface does not provide direct access to the binding control not does it describe the property in the underlying data object to which a control is being bound.

I did find a "solution" to this scenario.  Actually, a better word would be "hack".  It hestitate to describe it - it's that ugly.  It does work, but I'm really hoping that someone provides a cleaner solution.  Until then, it will have to do.

W James

WJamesLord

Loading...
Joined on 06-19-2007
Posts 104
07-07-2008 12:53 AM
Marked as Answer
Re: Re: Re: Are Data Binding Details Accessible in Beta 2?
 

Hello, I don't quite understand your scenario. Can you give a sample? If you want to modify some properties of Controls based on the binding source, I think you can just use value converters. For example, bind a Grid's Background to a person's age:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Person person = (Person)value;
if(person.Age > 20)
{
return new SolidColorBrush(Colors.Blue);
}
return new SolidColorBrush(Colors.Red);
}

If you want to validate the user input, you should rather do the validation on the data source, in the set methods, which is more secure.

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,688
07-07-2008 4:38 PM
Re: Re: Re: Re: Are Data Binding Details Accessible in Beta 2?

I'm binding the content section of a XAML to an object constructed from a dynamically-created type.  The type is generated by a data serializer that I use to transport values to/from web services.  Each bound control in the content section is linked to one property in the type.  The bindings are defined in the XAML in a typical fashion.  So far pretty straight forward stuff.

Now the fun.  I need to cross-reference the bound controls with meta-data stored in the serializer.  There is separate meta-data for each property stored in the data object.  However, when binding to the Text property of a TextBox (or a similar such property for other controls), a custom IValueConverter only provides the binding value and type.  It doesn't provide the affected control or the related bound data object or the related property in the bound data object.  So as it stands, a IValueConverter doesn't really meet my needs...  when used as a XAML-driven converter.

However, by moving the data binding and custom converters out of the XAML and into the code behind, I can then pass a "binding details" object as a parameter to each Binding.  This provides the necessary information and solves my issues.

While my solution works, I would have prefered to leave the bindings in the XAML and then reference a "GetBinding" method for each bound control to access the binding information.  Unfortunately, there is currently only a SetBinding method.  Perhaps in the future, its counterpart will be added.  For now, my bass ackwards approach will have to do.

Thanks to everyone for any replies to my posting.

W James

P.S. Yi-Lun, in your example, I see that you reference the value parameter as a class.  In my bindings, the value is always presented as a single property from the parent class.  Please explain how the value may be passed as a class.

 

 

WJamesLord

Loading...
Joined on 06-19-2007
Posts 104
07-07-2008 11:59 PM
Marked as Answer
Re: Re: Re: Re: Re: Are Data Binding Details Accessible in Beta 2?

Well, just create a binding without a path:

{Binding Converter={StaticResource yourConverter}}

For GetBinding, it does exist in WPF, but unfortunately it's unlikely to be supported in Silverlight 2... Maybe in the future, I can't promise you...

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,688
07-09-2008 8:06 AM
Re: Re: Re: Re: Re: Re: Are Data Binding Details Accessible in Beta 2?

Thanks, Yi-Lun.  That's a useful tidbit on binding.  I'll file it away for future development.

 

WJamesLord

Loading...
Joined on 06-19-2007
Posts 104
Microsoft Communities