大家的第一篇文章在这里:) 让我们开始与我使用的代码片段:如何绑定到Silverlight 4中的单例属性?
public MyClass : INotifyPropertyChanged
{
private static MyClass _instance;
public static MyClass Instance
{
get
{
if (_instance == null)
_instance = new MyClass();
return _instance;
}
}
private bool _myProperty;
public bool MyProperty
{
get
{
return _myProperty;
}
set
{
if (_myProperty!= value)
{
_myProperty= value;
NotifyPropertyChanged("MyProperty");
}
}
}
private MyClass() { ... }
}
正如你所看到的,它是一个单例类。 在我看来,我想绑定MyProperty上的控件。我最初的想法是使用类似导入MyClass的在我看来,一个静态的ressource:
<UserControl x:Class="Metrics.Silverlight.ChartView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:logic="clr-namespace:Metrics.Logic;assembly=Metrics.Logic">
<UserControl.Resources>
<logic:MyClass x:Key="myClass" />
</UserControl.Resources>
</UserControl>
,并将其绑定像这样:
<Button Margin="5" Click="btnName_Click" Visibility="{Binding Source={StaticResource myClass}, Converter={StaticResource visibilityConverter}, Path=MyAttribute, Mode=OneWay}">
当然,这种方法不会因为MyClass的构造函数的工作是私人的。我也无法使用x:static,因为它在Silverlight 4中不可用。
我一直在这个问题上停留的时间比我应该的要长得多...... 我该如何绑定MyProperty?
任何想法?
提前致谢!
嗨M.科普塞,我想尝试一下你的建议,但我必须承认,我不太明白如何解决这个问题。我仍然不熟悉silverlight/xaml编程。我会很感激,如果你能详细说明:) – Mathieu 2011-03-15 12:38:42
@Tigel:这与Elad的选项基本相同。我只是建议单独离开你的单身人士,但在你的控制本身(非静态)有一个属性返回单身人士。 – 2011-03-15 16:04:09
好的问题解决了。感谢您的帮助先生们! @Vladimir Dorokhov @Reed Copsey @Elad Katz – Mathieu 2011-03-15 17:16:43