views:

45

answers:

1

I'm new to silverlight, so I don't know if this is obvious or not.

I have a datagrid where the first column is a check box (named "Overridden"). The second column (named "ShowDetails") is a button which allows the user to expand the row to see a nested grid. If the "Overridden" check box is not checked, the "ShowDetails" button should be disabled.

The screen is correct when initially displayed. The "ShowDetails" button is disabled when the initial "Overridden" check box is unchecked. When I check the "Overridden" check box, the "overriddenFlag" is changed, however, the "ShowDetails" button remains disabled, instead of changing to 'enabled'.

Why doesn't the "IsEnabled" flag of the "ShowDetails" button change?

Here are the 2 columns from the xaml

        <sdk:DataGrid.Columns>

            <sdk:DataGridTemplateColumn Header="Overridden">
              <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <CheckBox IsChecked="{Binding OverriddenFlag,Mode=TwoWay}"
                            Click="Overridden_Click"
                            HorizontalAlignment="Center"
                        />
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                      <Button x:Name="ShowDetails" 
                          FontWeight="bold" FontSize="12"
                          Content="+" Click="ShowDetails_Click" 
                          IsEnabled="{Binding OverriddenFlag, Mode=OneWay}"
                                />
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

What is the proper way to enable/disable a button in a datagrid programmatically?

I've tried to do a variety of things I've seen here in Stack Overflow, such as INotifyProperty, a converter to hide the button (which didn't show the button when I checked the check box), and I've tried to do things in the Overridden_Click fct, such as FindName() - but have had no success.

A: 

The most likely problem you are having is that the object that the row is bound to does not implement the INotifyPropertyChanged. Without this interface being implemented your button has no way to learn of the change that the check box has made to the OverriddenFlag property.

Here is an example of how that is done:-

public class MyClass : INotifyPropertyChanged
{

    private bool _overriddenFlag;
    public bool OverriddenFlag
    {
        get { return _overriddenFlag; }
        set
        {
            _overriddenFlag= value;
            NotifyPropertyChanged("OverriddenFlag");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
AnthonyWJones
Thank you, that did the trick! You've really helped my learning curve - thank you so very very much.
Karen
FYI: one last comment, in case some one else finds themselves in the same predicament. I had tried using INotifyPropertyChanged but it didn't work. I suspect I didn't have mode=TwoWay. So thank you again for helping get the puzzle pieces to fall into place.
Karen