XAML 中的 WPF 数据绑定

发布于 2024-07-13 16:52:21 字数 500 浏览 5 评论 0原文

我当前项目中的数据绑定没有什么问题。 我有一个 ObservableCollection 我想绑定到一个 ListBox。

public ObservableCollection<GeoDataContainer> geoList = new ObservableCollection<GeoDataContainer>();

...稍后...

geoListBox.ItemsSource = geoList;

这段代码工作正常。 列表框有一个数据模板,一切看起来都很完美。

但我不想使用 C# 代码进行绑定。 我想在 XAML 代码中进行绑定。 我正在寻找几天,但我没有得到它。 这是两行 C# 代码,但要在 XAML 中存档它,如果不为我的集合创建我自己的类或添加 DataProvider 或资源或其他内容,似乎是不可能的。

难道就没有简单的办法吗?

I have little problem with databinding in my current project.
I have an ObservableCollection I want to bind to an ListBox.

public ObservableCollection<GeoDataContainer> geoList = new ObservableCollection<GeoDataContainer>();

...later...

geoListBox.ItemsSource = geoList;

This code works fine. The Listbox has a datatemplate and everything looks perfect.

But I don't want to use C# code for binding. I want to make the binding in the XAML Code.
I am searching for days but I don't get it. These are two lines C# code but to archive this in XAML it seems impossible without creating my own class for my collection or adding a DataProvider or resources or whatever.

Is there no easy way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生来就爱笑 2024-07-20 16:52:21

您所要做的就是公开集合并绑定到它。 例如,如果将其公开为:

public ICollection<GeoDataContainer> GeoList
{
    get { return geoList; }
}

您将能够将其绑定为:

<ListBox ItemsSource="{Binding GeoList}"/>

“技巧”是确保 ListBoxDataContext 是该类公开 GeoList 属性。

All you have to do is expose the collection and bind to it. For example, if you expose it as:

public ICollection<GeoDataContainer> GeoList
{
    get { return geoList; }
}

You will be able to bind to it as:

<ListBox ItemsSource="{Binding GeoList}"/>

The "trick" is to make sure that the DataContext of the ListBox is the class that exposes the GeoList property.

木緿 2024-07-20 16:52:21

另一个好方法是将 geoList 实例化为资源

<WindowResources>
  <l:GeoCollection x:Key="geoList"/>
</WindowResources>

然后你就可以了

GeoCollection geoList = FindResource("geoList") as GeoCollection;

当然,这是针对数据仅与视图相关的情况。 如果这与模型或模型视图相关,您可以使用 DataContext 并绑定到其属性。

Another good way would be instantiating geoList as a resource

<WindowResources>
  <l:GeoCollection x:Key="geoList"/>
</WindowResources>

Then you have

GeoCollection geoList = FindResource("geoList") as GeoCollection;

Of course, this is for cases when the data is related to the view only. If this is related to model or modelview, you use DataContext and bind to its properties.

花期渐远 2024-07-20 16:52:21

Kent 建议是可行的方法...

进一步说明,如果您不想将 DataContext 设置到列表中,您还可以使用另一种形式的绑定检索属性:

确保您的根控件有一个名称,即“根”

{Binding ElementName=Root, Path=GeoList}

Kent suggestion is the way to go...

On a further note, if you do not wish to set your DataContext to the list, you can also retrieve the property with an another form of binding:

Make sure your root control has a name, i.e. "Root"

{Binding ElementName=Root, Path=GeoList}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文