samedi 9 mai 2015

XAML C# Button IsEnabled property bound to static class property not responding to PropertyChanged

I am fairly new to XAML and WPF and have read numerous examples of how bind control properties but none seem to apply to my problem.

I have a static class Analyse that inherits INotifyPropertyChanged

Summary of the code below

class Analyse : INotifyPropertyChanged
{
    public static DataSet moodleData;  // Dataset containing the log data for analysis
    private static bool dataPresent = true;

    public static Boolean DataPresent
    {
        get { return dataPresent; }
        set
        {
            if (dataPresent != value)
            {
                dataPresent = value;
                NotifyStaticPropertyChanged("DataPresent");
            }
        }
    }

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
     = delegate { };

    private static void NotifyStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
    }


    #endregion

    public static void clearData()
    {
        try
        {
            moodleData.Clear();
            DataPresent = false;
        }
        catch { }
    }
}

My XAML includes the name space local

<Window x:Name="TheMainWindow" x:Class="MoodleLogAnalyse.MainWindow"
    xmlns="http://ift.tt/o66D3f"
    xmlns:x="http://ift.tt/mPTqtT" 
    DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"

    xmlns:local="clr-namespace:MoodleLogAnalyse"

    Title="MainWindow" Height="556.88" Width="793" WindowStartupLocation="CenterScreen">

And the button is bound correctly

<Button Name="OpenButton" Command="Open" 
       IsEnabled="{Binding Source={x:Static local:Analyse.DataPresent},
       Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"  
       Content="Open Grades" />

The property is definitely bound to the IsEnabled, changing the dataPresent definition manually in code enables and disables the button but a dynamic change such as switching between true and false such as calling the clear data method does not change the IsEnabled state of the button at run time.

The property change event is triggering as I have inserted break point to check.

I cannot see where I am going wrong with this! Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire