绑定到代码隐藏中的方法
我有一个用于 ListView
的自定义控件模板,它为每个记录添加了一行额外的行,这在 Window.Resources 中定义了类似的内容...
<ControlTemplate TargetType="ListBoxItem">
<Border>
<StackPanel>
<GridViewRowPresenter>
<TextBlock Name="myTextBlock" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
//Triggers here
</ControlTemplate.Triggers>
</ControlTemplate>
我的问题是我想将文本绑定在将 TextBlock
更改为与实例化时绑定到实际 ListBox
的不同 ItemsSource
。以编程方式绑定是不可能的。我尝试用 TextBlock
替换另一个 ListView
并绑定到方法,但我无法弄清楚如何使用 ObjectDataProvider
和绑定到我的代码后面的一个方法(其中包含一个会返回我想要绑定的内容列表的方法),但也遇到了问题。
快速一步一步,以防我不清楚:
我有一个
ListView
模板,为每条记录添加额外的行此
ListView
将绑定到(例如)Foo 对象的集合。问题是我想将额外的行绑定到与主
ListView
完全不同的项目源。我似乎无法在模板中执行此操作:/
那么 - 有没有一种方法可以直接绑定到我的代码后面定义的方法的结果,我可以在模板中引用该方法?
I have a custom control template for a ListView
that puts an extra line in for each record, thats defined something like this in Window.Resources...
<ControlTemplate TargetType="ListBoxItem">
<Border>
<StackPanel>
<GridViewRowPresenter>
<TextBlock Name="myTextBlock" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
//Triggers here
</ControlTemplate.Triggers>
</ControlTemplate>
My problem is that I want to bind the text in the TextBlock
to a different ItemsSource
than the one that will be bound to the actual ListBox
when its instantiated. Binding programatically is impossible. I've tried substituting the TextBlock
for another ListView
and binding to a method, but I couldn't work out how to use ObjectDataProvider
and bind to a method in my code behind (which contains a method that would return a list of things I want to bind too), but ran into problems with this as well.
A quick step by step in case I'm not being clear:
I have a
ListView
template that adds an extra row for each recordThis
ListView
will be bound to (say) a collection of Foo objects.Problem is I then want to bind the extra row to a completely different itemsource than the main
ListView
. It doesnt seem like I can do this from within my template :/
So - is there a way to straight up bind to the results of a method defined in my code behind, which I could reference in the template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这就是想法:
1)使用这样的项目模板创建绑定到
Foo
的列表框:2)创建使用这样的绑定到
完全不同的 itemsource
的列表框项目模板:3)在第二个列表框后面绘制第一个列表框。
如果项目数量相同(据我了解,这是您的情况),您将达到您想要的视觉效果。
希望有帮助。
编辑
此方法与您当前的模板不对应,但它是解决方案的变体。
Ok, that is the idea:
1) create list box which is bound to
Foo
with such item template:2) create list box which is bound to
completely different itemsource
with such item template:3) draw the first list box behind the second one.
If there are the same count of items (this is your case as I understand), you will achieve the visual effect you want.
Hope it helps.
EDIT
This method does not correspond your present template, but it is a variant of solution.