我有静态类'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。
请帮助我。 谢谢。
答案 0 :(得分: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 });
您只需在实例引用中添加静态引用。
答案 1 :(得分: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);