以编程方式创建 ListView 项的绑定

发布于 2024-11-07 02:01:44 字数 522 浏览 0 评论 0原文

我将以下 wpf 控件添加到 xaml 中:

<ListView Margin="22,80,271,12" Name="listView1" ItemsSource="{Binding}"  />

我知道如何以编程方式创建 ListView 对象。我唯一缺少的是如何将

ItemsSource="{Binding}"

带有代码的属性添加到该对象。我已经设法用 C# 添加列和 gridview 。我唯一缺少的是添加该属性 ItemsSource="{Binding}"

我尝试寻找答案 这里

I have the following wpf control added to xaml:

<ListView Margin="22,80,271,12" Name="listView1" ItemsSource="{Binding}"  />

I know how to create a ListView object programmatically. The only thing that I am missing is how could I add the property

ItemsSource="{Binding}"

with code to that object. I have already managed to add the columns and gridview with c#. The only thing that I am missing is to add that property ItemsSource="{Binding}"

I have tried looking for an answer here.

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

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

发布评论

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

评论(4

带上头具痛哭 2024-11-14 02:01:44

最短的应该是这样(XAML的直译):

listView1.SetBinding(ListView.ItemsSourceProperty, new Binding());

Shortest should be this (literal translation of XAML):

listView1.SetBinding(ListView.ItemsSourceProperty, new Binding());
江湖彼岸 2024-11-14 02:01:44
listView1.ItemsSource = listView1.DataContext as IEnumerable;
listView1.ItemsSource = listView1.DataContext as IEnumerable;
欲拥i 2024-11-14 02:01:44

这是您要找的吗?

Binding myBinding = new Binding();
myBinding.ElementName = "item-you-are-binding-to";
myBinding.Path = new System.Windows.PropertyPath("property-you-are-binding-to");
listView1.SetBinding(ContentProperty, myBinding);

Is this what you are looking for?

Binding myBinding = new Binding();
myBinding.ElementName = "item-you-are-binding-to";
myBinding.Path = new System.Windows.PropertyPath("property-you-are-binding-to");
listView1.SetBinding(ContentProperty, myBinding);
空心↖ 2024-11-14 02:01:44

您需要做的就是:

var binding = new Binding("DataContext");
binding.Source = listView1;
listView1.SetBinding(ListView.ItemsSourceProperty, binding);

All you need to do is this:

var binding = new Binding("DataContext");
binding.Source = listView1;
listView1.SetBinding(ListView.ItemsSourceProperty, binding);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文