Silverlight 3 DataBinding with ValueConverter: conditionally use the default value for property

Posted by eriksmith200 on Stack Overflow See other posts from Stack Overflow or by eriksmith200
Published on 2010-06-10T12:08:07Z Indexed on 2010/06/10 12:12 UTC
Read the original article Hit count: 265

Filed under:
|

I have a DatePicker for which I want to set the BorderBrush to SolidColorBrush(Colors.Red) if the SelectedDate is null. If a date has been filled in though, I want to just have the default BorderBrush. I still want to be able to style the default BorderBrush in Blend, so I don't want to hardcode the default borderbrush of the DatePicker. So basically:

xaml:

<controls:DatePicker BorderBrush="{Binding SelectedDate, RelativeSource={RelativeSource Self}, Converter={StaticResource BrushConverter}, Mode=OneWay}"/>

c#:

public class BrushConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            return value == null ?
                new SolidColorBrush(Colors.Red) : /* when value != null have the bound property use it's default value */
        }

is this possible?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about databinding