0

私は静的クラス「MappingService」を持っています。

public  class MappingService : INotifyPropertyChanged
{
    static readonly MappingService _Instance = new MappingService();
    public static MappingService Instance
    {
        get { return _Instance; }
    }

    public Efficiency Source { get; set; }
}

コードビハインドで ComboBox を作成します。コード ビハインドでItemsSource MappingService.Instance.Source
をバインドしたい。

comboBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("MappingService.Instance.Source") { Mode = BindingMode.TwoWay });

しかし、MappingService.Instance.Source にアクセスできません。

助けてください。ありがとうございました。

4

2 に答える 2

1

これは簡単な方法の 1 つです。設計によっては、これよりも優れたオプションがあるかもしれません。これを試して

  public  class MappingService : INotifyPropertyChanged
    {
    static readonly MappingService _Instance = new MappingService();
    public static MappingService Instance
   {
    get { return _Instance; }
    }

    public MappingService BindingObject 
    {
    get { MappingService._Instance; }
    }
     public Efficiency Source { get; set; }
   }

そしてあなたのxamlコード。

  comboBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("BindingObject.Source") { Mode = BindingMode.TwoWay });

インスタンス参照内に静的参照を追加するだけです。

于 2016-03-30T06:02:27.780 に答える
1

バインド方法は次のとおりです。

var propertyPath = new PropertyPath("Source");
var binding = new System.Windows.Data.Binding
{
     Path = propertyPath,
     Mode = BindingMode.TwoWay,
     Source = MappingService.Instance
};
BindingOperations.SetBinding(
    comboBox,
    System.Windows.Controls.ItemsControl.ItemsSourceProperty,
    binding);
于 2016-03-30T06:11:15.367 に答える