Tuesday, January 29, 2013

In Silverlight how do I set a buttons enabled state to be enabled based on items selected in a listbox?

In Silverlight how do I set a buttons enabled state to be enabled based on items selected in a listbox?

I have a dialog that contains a listbox and the customary ok, cancel buttons. I would like set the enabled state of the ok button to be enabled only if an item in the listbox has been select. I would like to do this with bindings rather than in the code behind.

I may have been going down the wrong route but I have being trying to do something like the following

IsEnabled="{Binding ElementName=ProjectList, Path=??? }"  

As you can probably see I have no idea what would go in the "Path"

Answers & Comments...

Answer: 1

If ProjectList is the name of the list box then you should be able to use SelectedItem.

You will need to bind through a converter that checks for the SelectedItem being null and returning false in that case.

So your XAML becomes:

IsEnabled="{Binding ElementName=ProjectList, Path=SelectedItem, Converter={StaticResource SelectedItemToBool}}"  

and the selector looks something like this:

public class SelectedItemToBool : IValueConverter {     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)     {         return value != null;     }      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)     {         throw new NotImplementedException();     } } 
by : ChrisFhttp://stackoverflow.com/users/59303




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog