如何绑定到singleton属性在Silverlight 4? [英] How to bind to a singleton property in Silverlight 4?

查看:104
本文介绍了如何绑定到singleton属性在Silverlight 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好第一篇文章在这里:) 让我们先从我使用code的一个片段:

Hi all first post here :) Let's start with a snippet of the code I'm using:

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在我看来:

As you can see, it's a singleton class. In my view, I want to bind a control on MyProperty. My initial idea was to import MyClass as a static ressource in my view using something like:

<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:静态因为它不是用在Silverlight 4

Of course, this approach won't work since MyClass constructor's is private. I also cannot use x:static since it's not available in Silverlight 4.

我已经被困在这个问题上的时间远远超过我应该... 我如何在绑定myProperty的?

I've been stuck on this problem far longer than I should have... How can I bind on MyProperty?

任何想法?

在此先感谢!

推荐答案

您可以有你的用户控件,在内部,通过它自己的财产暴露MyClass的实例,并在本地绑定到它自己的MyClass的实例。因为它是一个单例,这将永远是相同的实例。

You could have your UserControl, internally, expose the MyClass instance through it's own property, and bind locally to it's own "MyClass" instance. Since it's a Singleton, this will always be the same instance.

这篇关于如何绑定到singleton属性在Silverlight 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆