我對XAML和WPF相當新,並閱讀了大量如何綁定控件屬性的例子,但似乎沒有適用於我的問題。綁定到一個靜態屬性沒有響應PropertyChanged
我有一個靜態類分析,它繼承INotifyPropertyChanged的下面
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 { }
}
}
我的XAML代碼
摘要包括名字空間當地
<Window x:Name="TheMainWindow" x:Class="MoodleLogAnalyse.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"
xmlns:local="clr-namespace:MoodleLogAnalyse"
Title="MainWindow" Height="556.88" Width="793" WindowStartupLocation="CenterScreen">
而且按鈕被正確綁定
<Button Name="OpenButton" Command="Open"
IsEnabled="{Binding Source={x:Static local:Analyse.DataPresent},
Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Content="Open Grades" />
該屬性肯定與IsEnabled綁定,在代碼中手動更改dataPresent定義啓用和禁用該按鈕,但動態更改(如在真和假之間切換,如調用清除數據方法不會更改按鈕的IsEnabled狀態在運行時。
屬性更改事件正在觸發,因爲我插入了要檢查的斷點。
我看不到我在哪裏出錯了!任何幫助,將不勝感激。
有趣的評論,但不能幫助解決的問題! –
實際上,自WPF 4.5以來,它應該與StaticPropertyChanged事件一起工作。但是,你的綁定應該是'{Binding Path =(local:Analyse.DataPresent),...}'。 – Clemens
這似乎也沒有必要實施INotifyPropertyChanged。你可以改爲聲明你的類是靜態的:'static class Analyze {...}'。 – Clemens