Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Detecting binding failures
Message
De
22/12/2009 23:16:54
 
 
À
19/12/2009 05:06:30
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01439787
Message ID:
01440292
Vues:
39
>>>Hi,
>>>Given, say, this binding in XAML:
<TextBox Width="200" Text="{Binding Path=SelectedItem.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
where 'SelectedItem' may or may not have a 'Text' property.
>>>
>>>It would be ideal to have some method of binding the 'IsEnabled' property so that the control was disabled if the binding fails but I can't think of a way of implementing this in a generic sort of way (I can't even find a way of detecting when the binding fails except by programatically monitoring the Trace - which is kludgy at best and certainly not something to use in production code)
>>>
>>>Any suggestions?
>>>Viv
>>
>>Can't test right now... :-(
>>
>>But... IIRC correctly when the binding fails in the above scenario the Text property will return null? (If not this idea won't work)
>>
>>If it does bind to the text property with a null to Boolean converter and use that to set the IsEnabled property.
>>
>>HTH
>
>Hi,
>Good thought. But if you were referring to the TextBoxText property this is an empty string when the binding fails (which, of course, could equally be a valid value for a successful binding)
>
>I did plug in a value converter - but again that only fires when the binding succeeds.
>But I did get it to work using that plus setting the FallbackValue to false on the binding:
IsEnabled="{Binding Path=Text,Mode=TwoWay, Converter={StaticResource Conv},FallbackValue=False}"
Of course, this way, the ValueConverter can be pretty simple! :
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
>        {
>                return true;
>        }
:-}
>Thanks for starting me in a working direction. I'd still like to come up with a more 'generic' way of doing this though - other 'SelectedItems' might require input from controls other than a TextBox......

The FallbackValue trick was pretty slick, I haven't seen that one before.

I guess my question would be... why is the binding failing? Maybe something you can latch onto there like the type of the selected item?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform