C# WPF将组合框绑定到依赖项方法
我有一个带有存储库的页面,页面中有一个组合框,我想绑定到依赖项的某个方法C# WPF将组合框绑定到依赖项方法,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我有一个带有存储库的页面,页面中有一个组合框,我想绑定到依赖项的某个方法 public class MyPage : Page { private Dependency dep {get; set;} //method: GetAll() - returns IEnumerable<Foo> ... } 但在那之后,我不知道如何继续下去 显然,在代码背后,它将是 mycombobox.ItemsSource = dep.GetAll(); mycombobox.Displa
public class MyPage : Page
{
private Dependency dep {get; set;} //method: GetAll() - returns IEnumerable<Foo>
...
}
但在那之后,我不知道如何继续下去
显然,在代码背后,它将是
mycombobox.ItemsSource = dep.GetAll();
mycombobox.DisplayValuePath = "FooName";
mycombobox.SelectedValuePath = "FooId";
不知道什么是类
依赖关系
,但只添加另一个可以绑定到的属性如何:
public class MyPage : Page
{
public Dependency Dep { get; set; }
public IEnumerable<Foo> AllDeps
{
get { return Dep.GetAll(); }
}
}
仍然不起作用。绑定发生在
InitializeComponents()
方法之前或之后?在这种过于简单的方法中,Dep
应该在InitializeComponent之前初始化,因为绑定是在该方法执行期间建立的。您可以在AllDeps
getter中设置断点,以确定何时准确调用它。
public class MyPage : Page
{
public Dependency Dep { get; set; }
public IEnumerable<Foo> AllDeps
{
get { return Dep.GetAll(); }
}
}
<ComboBox ItemsSource="{Binding Path=AllDeps}" />
public class MyPage : Page
{
public Dependency Dep { get; set; }
public ObservableCollection<Foo> AllDeps { get; set; }
public MyPage()
{
AllDeps = new ObservableCollection<Foo>();
InitializeComponent();
// initialize Dep
foreach (var d in Dep.GetAll())
{
AllDeps.Add(d);
}
}
}