大家好,这是我在这里的第一篇帖子 :) 让我们从我正在使用的代码片段开始:
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作为静态资源:
<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? 有什么想法吗? 提前感谢!