0

I'm sure this is really easy, but I don't know how do it. I have a ComboBox and a Button, and I need to have the Button enabled only if the ComboBox has an item selected, i.e. if in the ComboBox has no elements showing, then the Button must be disabled. How can I do this?

I have tried doing the following:

IsEnabled="{Binding ElementName=mycombobox, Path=SelectedIndex}"/>

But it does not work. I'm using Silverlight 5.

Thanks in advance

David
  • 4,665
  • 4
  • 34
  • 60
Cyndy
  • 93
  • 1
  • 10

2 Answers2

0

There might be a more efficient way to do it, but I'd simply determine whether the ComboBox.SelectedItem is null in the SelectedIndexChanged event.

Tyler Daniels
  • 613
  • 6
  • 19
  • Yes, it is the most simply, but I was want to do it in xaml, maybe with a Converter...ok, thanks so much, I evaluate the choices. Regards – Cyndy Apr 21 '14 at 17:42
0

MSDN has something that may be of help to you here. It suggests you use a converter or a data trigger. I haven't tested this, but perhaps this will work?

<Window.Resources>
    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="IsEnabled" Value="True"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=comboBox1}" Value="{x:Null}">
                <Setter Property="UIElement.IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <ComboBox Name="comboBox1">
        <ComboBoxItem>One</ComboBoxItem>
        <ComboBoxItem>Two</ComboBoxItem>
        <ComboBoxItem>Three</ComboBoxItem>
    </ComboBox>

    <Button Style="{StaticResource MyButtonStyle}" Name="myButton" Content="Push me"/>
</Grid>

EDIT

I am under the impression that Cyndy has already figured all this out, but for any future readers...

As noted in the comments, you cannot do DataTriggers in Silverlight. You'll need to do a converter. Here is another post that may help. In essence, you'll need to have your XAML set something like:

<Button Content="MyButton" IsEnabled="{Binding SelectedItem, ElementName=comboBox1, Converter={StaticResource myConverter}}"/>

and then in your code-behind, you'll need something like:

public class MyConverter : 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();
    }
}
David
  • 4,665
  • 4
  • 34
  • 60
  • "{b:EvalBinding [lb.SelectedIndex] > -1}" is great! but it not Works in Silverlight...I'm working in a Converter, thanks so much!! – Cyndy Apr 21 '14 at 17:39
  • That part of the answer refers to a third-party binding extension. Look at the section above it. – David Apr 21 '14 at 17:41
  • ok, I see, I try use it in Silverlight but some things are different ` ` Style.Triggers not found and anything similar to this – Cyndy Apr 21 '14 at 17:49
  • I see--it appears Silverlight does not have DataTriggers as noted [here](http://stackoverflow.com/questions/3529508/what-is-the-replacement-for-datatrigger-in-silverlight). Sorry. I guess you're stuck with writing a converter. – David Apr 21 '14 at 17:56