Mat*_*ieu 9 .net c# silverlight mvvm silverlight-4.0
嗨所有的第一篇文章在这里:)让我们从我正在使用的代码片段开始:
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() { ... }
}
Run Code Online (Sandbox Code Playgroud)
如你所见,它是一个单身人士类.在我看来,我想在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>
Run Code Online (Sandbox Code Playgroud)
然后像这样绑定它:
<Button Margin="5" Click="btnName_Click" Visibility="{Binding Source={StaticResource myClass}, Converter={StaticResource visibilityConverter}, Path=MyAttribute, Mode=OneWay}">
Run Code Online (Sandbox Code Playgroud)
当然,这种方法不起作用,因为MyClass构造函数是私有的.我也不能使用x:static,因为它在Silverlight 4中不可用.
我一直坚持这个问题的时间远远超过我应该拥有的......我怎样才能在MyProperty上绑定?
有任何想法吗?
提前致谢!
我建议添加额外的类MyClassProvider:
public class MyClassProvider
{
public MyClass MyClass { get { return MyClass.Instance; } }
}
Run Code Online (Sandbox Code Playgroud)
您可以将此类放在任何位置并绑定到其MyClass属性的实例.