我有如下所述的類結構。使用「僅獲取」靜態屬性作爲綁定源
//On the design aspects, I know It may not be the advisable approach,
//but something of this kind is only required.
/// <summary>
/// Paper Class
/// </summary>
public class Paper
{
public string PaperName { get; set; }
public bool IsPending { get; set; }
}
/// <summary>
/// PaperChecking class, Individual papers will be processed here.
/// </summary>
public class PaperChecking
{
public static List<Paper> ListPapers { get; set; }
public static void AddPapers()
{
ListPapers = new List<Paper>();
ListPapers.Add(new Paper() { PaperName = "Paper1", IsPending = false });
ListPapers.Add(new Paper() { PaperName = "Paper2", IsPending = false });
ListPapers.Add(new Paper() { PaperName = "Paper3", IsPending = false });
ListPapers.Add(new Paper() { PaperName = "Paper4", IsPending = false });
ListPapers.Add(new Paper() { PaperName = "Paper5", IsPending = false });
}
public static bool IsCheckingPending
{
get
{
//List has items and it is not null, so intentionally removed the checks.
return ListPapers.Count(paper => paper.IsPending == true) > 0 ? true : false;
}
}
}
/// <summary>
/// This class will select papers for processing
/// </summary>
public class ChangePaperSetting
{
public void SelectPaper(string paperName)
{
//It can be assumed that Paper object will never be NULL
PaperChecking.ListPapers.FirstOrDefault(paper => paper.PaperName.Equals(paperName)).IsPending = true;
}
}
現在, 我想用財產PaperChecking.IsCheckingPending在我的WPF窗口中顯示的一些控制。我與我的控件的可見性綁定了相同的屬性。預計第一次窗口加載行爲時,因爲Collection已經存在。但是,當時我改變如下Paper對象的掛起狀態運行時:
ChangePaperSetting changePaperSetting = new ChangePaperSetting();
changePaperSetting.SelectPaper("Paper1");
changePaperSetting.SelectPaper("Paper2");
changePaperSetting.SelectPaper("Paper5");
在我的收藏,現在我已經IsPending爲真論文。因此,現在PaperChecking.IsCheckingPending將返回TRUE,並根據我的控件應該現在可見。
在一個普通的對象中,我可以實現INotifyPropertyChanged,但在上面的情況下,我沒有屬性上的Setter。有沒有這樣做的方法或任何其他使用相同的類結構的簡潔方法。
//-------------------------------------------------------------------------------//
更新
正如喬希建議,我想是這樣的:
/// <summary>
/// Paper Class
/// </summary>
public class Paper : INotifyPropertyChanged
{
public string PaperName { get; set; }
private bool isPending;
public bool IsPending
{
get
{
return isPending;
}
set
{
if (isPending != value)
{
isPending = value;
PropertyChanged(this, new PropertyChangedEventArgs("IsPending"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
/// <summary>
/// PaperChecking class, Individual papers will be processed here.
/// </summary>
public class PaperChecking : Control
{
public static List<Paper> listOfPapers { get; set; }
public static bool IsCheckingPending
{
get
{
//List has items and it is not null, so intentionally removed the checks.
try
{
return listOfPapers.Count(paper => paper.IsPending == true) > 0 ? true : false;
}
catch (Exception ex) { return false; }
}
}
public static event PropertyChangedEventHandler PropertyChanged;
public static void PendingStatusChanged(object sender,PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsPending")
{
//If I keep it static, It given Null Reference Error
//and If I implement INotifyPropertyChanged interface
//in this Class, it gives compilation error because
//I am doing so in my Static property.
PropertyChanged(null,new PropertyChangedEventArgs("IsCheckingPending"));
}
}
}
/// <summary>
/// This class will select papers for processing
/// </summary>
public class ChangePaperSetting
{
public static void AddPapers()
{
var listOfPapers = new List<Paper>();
for (int i = 0; i < 5; i++)
{
var paper = new Paper() { PaperName = "Paper"+i.ToString(),
IsPending = false };
paper.PropertyChanged+=PaperChecking.PendingStatusChanged;
listOfPapers.Add(paper);
}
PaperChecking.listOfPapers = listOfPapers;
}
public void SelectPaper(string paperName)
{
//It can be assumed that Paper object will never be NULL
PaperChecking.listOfPapers.FirstOrDefault(paper => paper.PaperName.Equals(paperName)).IsPending = true;
}
}
這是我的XAML代碼:
<Window xmlns:my="clr-namespace:LearningWpf" x:Class="LearningWpf.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="300" Width="300"
>
<Window.Resources>
<my:PaperChecking x:Key="paperChecking"/>
<BooleanToVisibilityConverter x:Key="bvc" />
</Window.Resources>
<StackPanel>
<Button Name="btn1" Content="Button1" Height="20" Width="80" Click="btn1_Click"></Button>
<Button Name="btn2" Content="Button2" Height="20" Width="80" Click="btn2_Click"></Button>
<Button Name="btn3" Content="Button3" Height="20" Width="80"
Visibility="{Binding Source={StaticResource paperChecking},
Path=IsCheckingPending,
Converter={StaticResource bvc}}"></Button>
</StackPanel>
這裏是我的CodeBehind.c s
public partial class Window4 : Window
{
public Window4()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
ChangePaperSetting.AddPapers();
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
var v = PaperChecking.listOfPapers.FirstOrDefault(paper =>
paper.PaperName == "Paper1");
v.IsPending = true;
}
}
但是這段代碼給出了錯誤,righlty,因爲我使用了靜態變量而沒有初始化它。如果有任何更正或任何其他方法來實現相同的目標。非常感謝您的幫助。
其實我想過這樣做,但沒有。更新屬性的接口在這裏很大,並且在我的對象屬性的生命週期中會經常更改。 – AjayK 2012-02-26 11:21:13
實際上,Objects可以從多個接口更新,它們不會調用相同的方法,它們引用PAPER對象並直接更新屬性,而不是調用PaperChecking類中的方法。如果情況確實如此,那麼這個問題就相當簡單直接的實施。 – AjayK 2012-02-26 11:44:34
無論何時何地添加新紙張,我都必須分配此處理程序。但同一個集合正在跨越多個接口進行更新,這似乎相當困難。有沒有其他的選擇依賴性屬性。 – AjayK 2012-02-26 13:14:46