绑定到代码隐藏中的方法

发布于 2024-08-18 14:39:43 字数 1058 浏览 8 评论 0原文

我有一个用于 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 record

  • This 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 技术交流群。

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

发布评论

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

评论(1

各自安好 2024-08-25 14:39:43

好的,这就是想法:

1)使用这样的项目模板创建绑定到 Foo 的列表框:

-------------
| DATA HERE |
|           |
-------------

2)创建使用这样的绑定到 完全不同的 itemsource 的列表框项目模板:

-------------
|           |
| DATA HERE |
-------------

3)在第二个列表框后面绘制第一个列表框。

如果项目数量相同(据我了解,这是您的情况),您将达到您想要的视觉效果。
希望有帮助。

编辑
此方法与您当前的模板不对应,但它是解决方案的变体。

Ok, that is the idea:

1) create list box which is bound to Foo with such item template:

-------------
| DATA HERE |
|           |
-------------

2) create list box which is bound to completely different itemsource with such item template:

-------------
|           |
| DATA HERE |
-------------

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.

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