2017-10-16 53 views
-3

我有一个简单的WPF应用程序,具有一个属性Generator然后具有ObservableCollection属性DataList的App类。Binding.Source属性的正确语法是什么?

我想将DataList绑定到ListBox。下面的语法是正确的:

<ListBox ItemsSource="{Binding Property=Generator.DataList, Source="{x:Static local:App:Current}" }" /> 

但下面是错误的:

<ListBox ItemsSource="{Binding Property=Generator.DataList, Source="TheApp" }" /> 

其中,该app是主窗口类的公共属性,定义如下:

public App TheApp {get; set;} = (App)Application.Current; 

那么,什么是正确的语法来源,我在哪里可以找到最好的解释?谢谢。

+0

你试试我的建议还是发生了什么事? – mm8

回答

0

尝试这种情况:

<ListBox ItemsSource="{Binding Path=TheApp.Generator.DataList, RelativeSource={RelativeSource AncestorType=Window}}" /> 

类(MainWindow)是在源和属性(TheApp)是路径的一部分。

数据绑定概述:https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview

如何:指定绑定源:https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-specify-the-binding-source

Binding.Path物业:https://msdn.microsoft.com/en-us/library/system.windows.data.binding.path(v=vs.110).aspx