gpt4 book ai didi

c# - 以编程方式绑定(bind) ItemsSource

转载 作者:太空狗 更新时间:2023-10-29 19:39:33 25 4
gpt4 key购买 nike

这在 C# 代码中的等价物是什么?

<ListView
x:Name="taskItemListView"
DataContext="{Binding SelectedItem, ElementName=itemListView}"
ItemsSource="{Binding taskItems}">
...
</ListView>

我已经尝试了下面的代码,但它似乎不起作用......

Binding b = new Binding();
b.Path = new PropertyPath("taskItems");

DependencyProperty dp = DependencyProperty.Register("itemsSource", typeof(object), typeof(object), null);
BindingOperations.SetBinding(taskItemListView, dp, b);

编辑:

根据@sa_ddam213 的回答,这有效:

Binding dataContextBinding = new Binding();
dataContextBinding.Path = new PropertyPath("SelectedItem");
dataContextBinding.Source = itemListView;
BindingOperations.SetBinding(taskItemListView, ListView.DataContextProperty, dataContextBinding );

Binding sourceBinding = new Binding();
sourceBinding.Path = new PropertyPath("taskItems");
BindingOperations.SetBinding(taskItemListView, ListView.ItemsSourceProperty, sourceBinding );

最佳答案

像这样的东西应该可以工作:

BindingOperations.SetBinding(taskItemListView, ListView.DataContextProperty, new Binding("SelectedItem") { Source = itemListView});
BindingOperations.SetBinding(taskItemListView, ListView.ItemsSourceProperty, new Binding("taskItems") { Source = this });

注意:“Source = this”this 等于持有taskItemsSelectedItem

的类

关于c# - 以编程方式绑定(bind) ItemsSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13898654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com