John
John

Reputation: 1477

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"

Upvotes: 0

Views: 323

Answers (1)

ChrisF
ChrisF

Reputation: 137128

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();
    }
}

Upvotes: 4

Related Questions