我有如下所述的类结构。使用“仅获取”静态属性作为绑定源
//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