RadButton.IsEnabled won't bind to RadComboBox.SelectedItem via an IValueConverter

2 posts, 1 answers
  1. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 11 Apr 2012 Link to this post

    I have two RadButtons, a RadComboBox, and a ListBox on a UserControl. I'm trying to bind the IsEnabled property of the Add button to the SelectedItem property of the combo box using a ValueConverter. My xaml looks like so:

    <UserControl.Resources>
        <local:SelectedItemToBoolConverter x:Key="SelectedItemToBool"/>
    </UserControl.Resources>
      
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
           <sdk:Label Content="Field Value:"/>
            <Grid Margin="0 0 0 4">
                <telerik:RadComboBox x:Name="fieldComboBox"                                    
                                     DisplayMemberPath="Name"
                                     SelectedValuePath="Name"/>
            </Grid>
            <Grid Margin="0 0 0 4">
            <ListBox x:Name="listBox" Height="150"
                         DisplayMemberPath="Name"
                         />
            </Grid>
            <StackPanel Orientation="Horizontal">
                <telerik:RadButton x:Name="addButton"
                                   Height="25"
                                   Width="75"
                                   Margin="0 0 2 0"
                                   Content="Add"
                                   IsEnabled="{Binding ElementName=fieldComobBox, Path=SelectedItem, Converter={StaticResource SelectedItemToBool}}"
                                   Click="addButton_Click" />
                <telerik:RadButton x:Name="deleteButton"
                                   Height="25"
                                   Width="75"
                                   Margin="0 0 2 0"
                                   Content="Delete"
                                   IsEnabled="{Binding ElementName=listBox, Path=SelectedItem, Converter={StaticResource SelectedItemToBool}}"
                                   Click="deleteButton_Click" />
            </StackPanel>
    </Grid>

    And my SelectedItemToBoolConverter class looks like this:

    public class SelectedItemToBoolConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value != null;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

    This works fine when binding the IsEnabled property of the Delete button to the SelectedItem property of the ListBox, but does not execute when binding the IsEnabled property of the Add button to the SelectedItem property of the RadComboBox. 

    Is there a reason this functionality does not work between those two controls?

    Thanks.

    EDIT: This code also works as is if I replace the RadComboBox with a standard silverlight ComboBox.


  2. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 17 Apr 2012 Link to this post

    Hi Andrew,

    It seems that the name of the combobox is wrong in the Binding:

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

    After fixing this, there is no problem in binding the IsEnabled property of the button. I've attached my test project for a reference.

    All the best,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top